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
udi08s
source share2 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
Mark rowland
source shareThe 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
Vinay
source share