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

HTML I'm trying to work with:

<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); 

Conclusion: Blank

+7
selenium selenium-webdriver
source share
2 answers

In order to select the input value, you will need to use the xpath selector, since using by.id will find the select element since they have the same id - this is bad practice as they really have to be unique. Anyway, try:

 driver.findElement(By.xpath("//input[@id='GlobalDateTimeText']")).getAttribute(‌​"value"); 
+6
source share

The code should be something like this

 WebElement dropDown = driver.findElement(By.id("GlobalDateTimeText")); Select sel = new Select(dropDown); List<WebElement> values = sel.getOptions(); for(int i = 0; i < values.size(); i++) { System.out.println(values.get(i).getText()); } 
0
source share

All Articles