Home > Net >  Loop Option Values in the dropdown
Loop Option Values in the dropdown

Time:05-24

I want to loop the value options displayed in the dropdown menu in an HTML web page. The option values seems to be in alpha numeric for which i write the code below, it is reading all the values within correctly, but i am not able to loop it continuously. Its showing the error stating "AttributeError: 'WebElement' object has no attribute 'select_by_value'"

Condition 1: If option value is 0 the driver should close Condition 2: If not it should read the next option value and work accordingly.

Any Guide through this will be appreciated.

select_box = driver.find_element(By.ID, "<ID_Name>") options = [x for x in select_box.find_elements_by_tag_name("option")] for element in options: i = element.get_attribute("value") print(i) if i == 0: driver.close() continue else: select_box.select_by_value(str(i))

CodePudding user response:

The method .select_by_value() cannot be used on webelements directly, you have first to import the following library

from selenium.webdriver.support.ui import Select

and then do one of the following

Select(select_box).select_by_index(...)
Select(select_box).select_by_value(...)
Select(select_box).select_by_visible_text(...)
  • Related