简体   繁体   中英

Error by getting selected option using Selenium Web Driver with Python

In order to output my already selected option I coded:

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

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.

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

should be driver.find_element . Note the s in find_element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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