Error by getting selected option using Selenium Web Driver with Python

Question:

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

Asked By: mathandlogic

||

Answers:

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)
Answered By: F.Hoque

driver.find_elements returns list of WebElements

should be driver.find_element. Note the s in find_element

Answered By: Akzy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.