The HTML I am trying to operate on:
<select id="GlobalDateTimeDropDown" class="combo"
onchange="CalculateGlobalDateTime('Time',this.value)" name="GlobalDateTimeDropDown">
<option value="+5.5" selected="selected"> … </option>
<option value="+12"> … </option>
<option value="+8"> … </option>
<option value="+2"> … </option>
</select>
<input id="GlobalDateTimeText" class="combo" type="text" name="GlobalDateTimeText" value="" style="width:215px;padding:2px;" readonly="readonly"></input>
Java Code:
WebElement values=driver.findElement(By.id("GlobalDateTimeText")).getAttribute("Value");
System.out.println(values);
Output: Blank
Using Selenium Web Driver to retrieve value of a HTML input. We can get the value of a HTML input with Selenium webdriver. This is achieved with the help of the getAttribute () method.
Selenium Automation Testing Testing Tools We can get the attribute value of a web element with Selenium webdriver using the method getAttribute and then pass the attribute for which we want to get the value as a parameter to that method. In an html code, an element is defined with attributes and its values in a key-value pair.
The getAttribute () method is declared in the WebElement interface, and it returns the value of the web element’s attribute as a string. For attributes having boolean values, the getAttribute () method will return either true or null.
QAs need to locate the web elements first and then call the getAttribute () method by specifying the attributes for which values are required. One can quickly refer to this guide on locators in Selenium to understand how web elements can be located.
To select the input value you'll need to use an xpath selector as using by.id
will find the select element as they share the same id
- which is bad practice as they really should be unique. Anyway, try:
driver.findElement(By.xpath("//input[@id='GlobalDateTimeText']")).getAttribute("value");
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