Using Selenium WebDriver to retrieve the value of an HTML input
JavaSeleniumSelenium WebdriverJava Problem Overview
In the HTML of a web application there is the following code:
<input type="text" name="prettyTime" id="prettyTime" class="ui-state-disabled prettyTime" readonly="readonly">
A string displaying the time is actually shown on the page.
In Selenium WebDriver, I have a WebElement
object referring to the <input>
using:
WebElement timeStamp = waitForElement(By.id("prettyTime"));
I want to get the value of the WebElement
, or, in other words, what is printed on the page. I tried all the WebElement
getters and nothing has been retrieving the actual value that the user sees.
Java Solutions
Solution 1 - Java
Try element.getAttribute("value")
The text
property is for text within the tags of an element. For input elements, the displayed text is not wrapped by the <input>
tag, instead it's inside the value
attribute.
Note: Case matters. If you specify "Value", you'll get a 'null' value back. This is true for C# at least.
Solution 2 - Java
You can do it like this:
webelement time = driver.findElement(By.id("input_name")).getAttribute("value");
This will give you the time displaying on the webpage.
Solution 3 - Java
With Selenium 2, I usually write it like this:
WebElement element = driver.findElement(By.id("input_name"));
String elementval = element.getAttribute("value");
Or
String elementval = driver.findElement(By.id("input_name")).getAttribute("value");
Solution 4 - Java
For Python bindings it will be:
element.get_attribute('value')
Solution 5 - Java
As was mentioned before, you could do something like this:
public String getVal(WebElement webElement) {
JavascriptExecutor e = (JavascriptExecutor) driver;
return (String) e.executeScript(String.format("return $('#%s').val();", webElement.getAttribute("id")));
}
But as you can see, your element must have an id
attribute, and also, jQuery on your page.
Solution 6 - Java
Following ragzzy's answer, I use
public static string Value(this IWebElement element,
IJavaScriptExecutor javaScriptExecutor)
{
try
{
string value = javaScriptExecutor.ExecuteScript("return arguments[0].value", element) as string;
return value;
}
catch (Exception)
{
return null;
}
}
It works quite well and does not alter the DOM.
Solution 7 - Java
Use
element.GetAttribute("value");
Even though if you don't see the "value" attribute in the HTML DOM, you will get the field value displayed in the GUI.
Solution 8 - Java
If the input value gets populated by a script that has some latency involved (e.g. AJAX call) then you need to wait until the input has been populated. E.g.
var w = new WebDriverWait(WebBrowser, TimeSpan.FromSeconds(10));
w.Until((d) => {
// Wait until the input has a value...
var elements = d.FindElements(By.Name(name));
var ele = elements.SingleOrDefault();
if (ele != null)
{
// Found a single element
if (ele.GetAttribute("value") != "")
{
// We have a value now
return true;
}
}
return false;
});
var e = WebBrowser.Current.FindElement(By.Name(name));
if (e.GetAttribute("value") != value)
{
Assert.Fail("Result contains a field named '{0}', but its value is '{1}', not '{2}' as expected", name, e.GetAttribute("value"), value);
}
Solution 9 - Java
Java users:
To get what is printed on the page, we need to use the getText() method.
getText() method
The getText() method returns the visible inner text of a web element.
getAttribute() method
On the other hand, the getAttribute() method fetches the value of the attribute we wish to retrieve.
Example:
<input name="Title" type="text" value="LambdaTest" /> Welcome to LambdaTest </input>
getText()
driver.findElement(By.name("Title")).getText();
Output of above code => Welcome to LambdaTest
getAttribute():
-
driver.findElement(By.name("Title")).getAttribute("value");
Output of above code => LambdaTest
-
driver.findElement(By.name("Title")).getAttribute("type");
Output of above code => text
Source: Difference between getText() And getAttribute() in Selenium WebDriver
Solution 10 - Java
This is kind of hacky, but it works.
I used JavascriptExecutor
and added a div
to the HTML, and changed the text in the div
to $('#prettyTime').val()
I then used Selenium to retrieve the div
and grab its value. After testing the correctness of the value, I removed the div that was just created.