Matching text in dropdown options using Python and Selenium

Feedback


Solution 1:

Considering your interest in text selection.

Modifying the value of option’s attribute to option.get_attribute(‘value’).

    data.append(option.text) #this will add 'Optis Cellular Technology','Orange',etc to data

Replace the value of “{}” with “//select/option[@value = {}]”.

    if a != '':   #to skip the first option because it's empty
      your_choice = driver.find_element_by_xpath("//select/option[text()='{0}']".format(a))
      your_choice.click() 


Solution 2:


In order to choose based on text that is visible, you can use the method

select_by_visible_text

provided by

python

. Simply capture the element

option

using the specified code.

select = Select(driver.find_element_by_id("option_id"))

If you prefer to use a different selector, you can simply utilize the function.

select.select_by_visible_text("visible_text")

Frequently Asked Questions