简体   繁体   English

使用 Selenium Web 驱动程序和 Python 获取所选选项时出错

[英]Error by getting selected option using Selenium Web Driver with Python

In order to output my already selected option I coded:为了 output 我已经选择的选项我编码:

Select(driver.find_elements(By.XPATH,'path_to_select/select/option'))
selected_option = select.first_selected_option
print(selected_option)

Error Message:错误信息:

raise UnexpectedTagNameException( selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on elements, not on引发 UnexpectedTagNameException(selenium.common.exceptions.UnexpectedTagNameException:消息:Select 仅适用于元素,不适用于

Because you are selecting a list of elements.因为您正在选择元素列表。 So you can use driver.find_element() instead of driver.find_elements() to select a single element.因此,您可以使用driver.find_element()而不是 driver.find_elements() 来 select 单个元素。

select = Select(driver.find_element(By.XPATH,'path_to_select/select/option'))
selected_option = select.first_selected_option
print(selected_option)

driver.find_elements returns list of WebElements driver.find_elements返回WebElements列表

should be driver.find_element .应该是driver.find_element Note the s in find_element注意find_element中的s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM