Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value from <input> tag using java in Selenium web driver when the Value attribute is generated dynamically

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

like image 344
udi08s Avatar asked Jul 31 '13 09:07

udi08s


People also ask

How to get the value of a HTML input with Selenium WebDriver?

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.

How do I get the value of an attribute in selenium?

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.

How do I get the value of a webelement attribute?

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.

How to locate web elements in selenium using QAS?

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.


Video Answer


1 Answers

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");
like image 83
Mark Rowlands Avatar answered Oct 12 '22 03:10

Mark Rowlands