• Home
  • About
    • Jang photo

      Jang

      Jang's blog

    • Learn More
    • Email
    • Facebook
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

How to select option in selenium webdriver using python

23 Dec 2017

Reading time ~1 minute

At first, I tried to click the option like this:

select_elem = driver.find_element_by_xpath("//select[@name='" + select_name + "']")
select_elem.click()

option_elem = select_elem.find_element_by_xpath("./option[contains(text(),'" + option_text + "')]")
option_elem.click()

or

select_elem = driver.find_element_by_xpath("//select[@name='" + select_name + "']")

for option in select_elem.find_elements_by_tag_name('option'):
    if option.text == option_text:
        option.click() # select() in earlier versions of webdriver
        break

But, click functionality does not work consistently. so, I changed the code like this:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_xpath("//select[@name='"+select_name+"']"))
select.select_by_visible_text(option_text)

It works well!



selenium Share Tweet +1