[英]Unable to select element by xpath from dropdown menu with Selenium in Python
I'm having a hard time trying to select an element from a dropdown menu with Selenium. 我很难尝试使用Selenium从下拉菜单中选择一个元素。
Inspecting the page it looks like this: 检查页面看起来像这样:
<select id="numbers" name="numbers" style="display: inline-block;">
<option value="all">all numbers</option>
<option value="c4ca4238a0b923820dcc509a6f75849b">One</option>
<option value="c81e728d9d4c2f636f067f89cc14862c">Two</option>
<option value="34173cb38f07f89ddbebc2ac9128303f">Three</option>
</select>
The code I'm using is: 我使用的代码是:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="numbers"]/option[3]'))
)
element.click();
But I'm receiving the error: 但是我收到错误:
EC.element_to_be_clickable((By.XPATH, '//*[@id="numbers"]/option[3]'))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/7q/ttwklc8j10x_8q7yy8t2scpw0000gn/T/tmpYJKH0R/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///var/folders/7q/ttwklc8j10x_8q7yy8t2scpw0000gn/T/tmpYJKH0R/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/7q/ttwklc8j10x_8q7yy8t2scpw0000gn/T/tmpYJKH0R/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/7q/ttwklc8j10x_8q7yy8t2scpw0000gn/T/tmpYJKH0R/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///var/folders/7q/ttwklc8j10x_8q7yy8t2scpw0000gn/T/tmpYJKH0R/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
For what I'm able to understand it's failing to locate the xpath, even if i double-checked copy/pasting it from Chrome. 对于我能够理解的是,即使我从Chrome仔细检查了复制/粘贴操作,也无法找到xpath。
What am I missing? 我想念什么? Thank you for your help.
谢谢您的帮助。
As, you have to select option from Select control, it is better to use Select class as shown below: 由于必须从Select控件中选择选项,因此最好使用Select类,如下所示:
select = Select(driver.find_element_by_id("numbers"))
select.select_by_visible_text("Two")
Let me know, whether above code works for you. 让我知道,以上代码是否对您有用。
I think first you need to click on that drop-down menu, than options will get displayed. 我认为您首先需要单击该下拉菜单,然后选项才会显示。
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="numbers"]')))
element.click();
and than perform click on //option[3]
然后点击
//option[3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.