Explain Codes LogoExplain Codes Logo

Using Selenium WebDriver to retrieve the value of an HTML input

javascript
webdriver
javascript-executor
selenium
Nikita BarsukovbyNikita Barsukov·Dec 3, 2024
TLDR

Extract an input's value in Selenium by using the getAttribute() method:

// Locating the HTML input element WebElement inputElement = driver.findElement(By.id("inputID")); // Getting the value using getAttribute() method String value = inputElement.getAttribute("value");

In essence, this straightforward getAttribute("value") method quickly extracts the content displayed in the input field.

When your Content is Dynamically Loaded

Sometimes, elements might load dynamically. This necessitates you to use the WebDriverWait in order to ensure the element gets fully loaded before fetching its value:

// Initialize WebDriverWait with a timeout of 10 seconds WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Wait until the target element becomes visible WebElement dynamicInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicInputID"))); // Finally, getting the value of the dynamically loaded input field String dynamicValue = dynamicInput.getAttribute("value"); // Note: Patience is a virtue, but code that waits is gold!

Jumping hoops with frames and iframes

For hidden treasures within an iframe, adjust your focus before attempting to retrieve values:

// Switching to the iframe context driver.switchTo().frame("frameID"); // Now, locating and getting the value from the iframe context WebElement inputInsideFrame = driver.findElement(By.id("inputInsideFrameID")); String frameInputValue = inputInsideFrame.getAttribute("value"); // Switching back to the main content. Remember: Always clean up after your mess! driver.switchTo().defaultContent();

Your Handy Assistant: JavascriptExecutor

In peculiar scenarios where direct WebDriver commands cannot fetch you the desired value, call upon the help of JavaScriptExecutor:

// Creating the JavascriptExecutor instance JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; // Locating the element first WebElement inputElementJS = driver.findElement(By.id("inputID")); // Retrieving the value using JavascriptExecutor String valueUsingJS = (String) jsExecutor.executeScript("return arguments[0].value", inputElementJS); // Note: Look, no hands! We're using JavaScript Magic!

Cautionary Measures & Good Practices

Handling those pesky nulls

To fend off NullPointerException, make sure the WebElement exists before calling the getAttribute() method.

WebElement inputElementNullSafe = driver.findElement(By.id("inputID")); if (inputElementNullSafe != null) { String valueNullSafe = inputElementNullSafe.getAttribute("value"); // Safety first, coding second! }

Readonly doesn't mean unreadable

Even if inputs have readonly attributes, their values can still be read using the getAttribute("value") method.

WebElement readonlyInput = driver.findElement(By.id("readonlyInputID")); String readonlyValue = readonlyInput.getAttribute("value"); // Even if it's readonly, we've got eyes everywhere!

Keep an eye on nested tags!

Complex DOM structures can be misleading. Tread with caution and consider using XPath or CSS selectors in such situations:

WebElement nestedInput = driver.findElement(By.xpath("//div[@class='wrapper']/input[@id='nestedInputID']")); String nestedValue = nestedInput.getAttribute("value"); // Note: It's not inception, it's just nested inputs! 🎬