The getAttribute() method helps to get the value of any attribute of a web element, which is returned as a String. If an attribute has a Boolean value, the method returns either True or null. Also, if there is no attribute, the method will return null.
We can get the value of a HTML input with Selenium webdriver. This is achieved with the help of the getAttribute() method. To retrieve the value of the field with tagname input, we have to pass the value as parameter to the getAttribute() method. Let us consider an html code for a html input.
Selenium offers a getText() method used to get the text of an element, i.e.; it can be used to read text values of an element from a web page. In this article, we will understand how to get the text of an element using the getText() method offered by Selenium WebDriver.
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.
You can do like this :
webelement time=driver.findElement(By.id("input_name")).getAttribute("value");
this will give you the time displaying on the webpage.
With selenium 2,
i usually write it like that :
WebElement element = driver.findElement(By.id("input_name"));
String elementval = element.getAttribute("value");
OR
String elementval = driver.findElement(By.id("input_name")).getAttribute("value");
For python bindings it will be :
element.get_attribute('value')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With