Using Selenium WebDriver to retrieve value of a HTML input

Last updated on : Jan 12, 2025 by Shubham Srivastava

If you want to know – Using Selenium WebDriver to retrieve value of a HTML input, How you can do this task? Then, you are at right place. In this step by step Selenium Tutorial, we will learn about retrieving the value of an HTML input element.

It is a common requirement in web automation and testing. Whether you’re validating the default values, checking dynamically populated fields, or verifying user input, understanding how to retrieve values using Selenium WebDriver is essential for every automation tester.

In this comprehensive guide will walk you through the process step by step, complete with code examples and clear explanations tailored for beginners. So, Let’s start with the solution first –

Top solution of using Selenium WebDriver to retrieve value of a HTML Input Element - LogicalDuniya.com

Solution 1: Step-by-Step Guide to Retrieve Value of a HTML input

Before starting with approach and solution, I hope, you have done the setting of Selenium WebDriver in your project and system. If not, go and check the guide to Setup of Selenium Framework and come back here and continue with solution.

Selenium provides multiple ways to solve this problem. Let’s first assume, below is the structure of Input Element –

HTML Structure of an Input Element

Here’s a sample HTML snippet for a simple input field:

<input id="username" type="text" value="defaultUser">

In this example:

  • The id attribute uniquely identifies the input field.
  • The value attribute holds the data stored in the field, which is defaultUser here.

Step 1: Launching a Web Page

Start by opening the web page containing the input field.

package com.logicalduniya.seleniumTutorials;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RetrieveInputValueExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.get("https://logicalduniya.com");
    }
}

Replace above "https://logicalduniya.com" with the URL of your target web page.


Step 2. Locate the Input Element

Use Selenium WebDriver to locate the input field using its id, name, or other attributes.

package com.logicalduniya.seleniumTutorials;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class RetrieveInputValueExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.get("https://logicalduniya.com");
        WebElement inputField = driver.findElement(By.id("username"));
    }
}

Here, we used the id attribute, which is the most reliable locator. Other locators like name, className, or CSS selectors can also be used depending on your scenario.


Step 3. Retrieve the Value of the Input

To get the value of the input field, use the getAttribute method:

package com.logicalduniya.seleniumTutorials;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class RetrieveInputValueExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.get("https://logicalduniya.com");

        WebElement inputField = driver.findElement(By.id("username"));

        String inputValue = inputField.getAttribute("value");
        System.out.println("The value of the input field is: " + inputValue);

    }
}

The getAttribute("value") method fetches the value property of the input element, regardless of whether it was set by default or entered dynamically.


Example Scenario 1: Validating Default Input Value

Imagine a login page where the username field is pre-filled with a default value for demonstration purposes. Using Selenium WebDriver to retrieve the value of a HTML input allows you to validate this default text:

if (inputValue.equals("defaultUser")) {
    System.out.println("Default value is correct.");
} else {
    System.out.println("Default value is incorrect.");
}

This approach ensures that the expected default values match the actual ones displayed on the web page.

Tips to avoid Selenium Script failures

  • Always ensure the input field is visible and enabled before interacting with it.
  • If the input field is inside an iframe, switch to the iframe using driver.switchTo().frame() before locating the element.
  • For dynamically loaded pages, use explicit waits to handle synchronization issues.

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement inputField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));

Complete Example Code

package com.logicalduniya.seleniumTutorials;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class RetrieveInputValue {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");

        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement inputField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));

        String inputValue = inputField.getAttribute("value");
        System.out.println("The value of the input field is: " + inputValue);

        driver.quit();
    }
}

Conclusion

Using Selenium WebDriver to retrieve the value of a HTML input is a fundamental skill for automation testers. By mastering this technique, you can efficiently validate default values, verify user inputs, and enhance your test scripts for dynamic web applications. With this guide, you now have a complete understanding of how to retrieve input values using Selenium WebDriver in Java.

This in-depth Selenium tutorial is part of our free Selenium Framework Mastery Series. In case, if you are also interested to learn about other important topics of Selenium, then check the link of Selenium Free Lessons.

Keep Thinking Logically..
Team LogicalDuniya.com
Cheers, Shubham !!

Shubham

Shubham Srivastava

Hey there, my name is Shubham and I'm working as an SDET at a US based FinTech company. I've more than 7 years of working experience and using my knowledge - I want make this platform super helpful for those, who want to solve any software development and testing related problems. Comment your query / request or suggestion in the below section, and I'll try to respond within 24 hours. Keep improving, Keep coding !!

Recommended Posts :


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close