Selenium can't find option form element by xpath

落花浮王杯 提交于 2019-12-11 20:29:37

问题


I'm trying to click on an option in a select menu via:

driver.implicitly_wait(30)
driver.find_element_by_xpath("//select[option/@value='today']").click()

but it doesn't work (prints None to the terminal)

the html looks like:

<select tabindex="0" class="itc_listBox mid"><option value="oneDay">1 Day</option><option value="sevenDays">7 Days</option><option value="fourteenDays">14 Days</option><option value="oneMonth">1 Month</option><option value="sixMonth">6 Months</option><option value="oneYear">1 Year</option><option value="" disabled="disabled">————————</option><option value="today">Today</option><option value="yesterday">Yesterday</option><option value="weekToDate">Week-to-Date</option><option value="last7Days">Last 7 Days</option><option value="lastWeek">Last Week</option><option value="monthToDate">Month-to-Date</option><option value="last30Days">Last 30 Days</option><option value="customDateRange">Custom </option></select>

I'm using the Firefox webdriver and I think the html is being generated by js


回答1:


Your XPath looks wrong. Try:

"//select/option[@value='today']"



回答2:


Select day = new Select(driver.findElement(By.cssSelector(".itc_listBox")));
day.selectByVisibleText("Today");

or

day.selectByValue("today")

You can try either of the above.



来源:https://stackoverflow.com/questions/7536012/selenium-cant-find-option-form-element-by-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!