1

I'm trying to select a dropdown option by part of the text.

How I would normally select text:

Select(browser.find_element_by_css_selector('selector')).select_by_visible_text("element text")

What I am trying to do:

Select(browser.find_element_by_css_selector('selector')).select_by_partial_visible_text("element text")

dot.Py
  • 5,007
  • 5
  • 31
  • 52

2 Answers2

2

I don't think you can do this with selecting via CSS. With xpath I think you can do it with something like this, but you have to change it to your xpath:

driver.find_elements_by_xpath("//*[contains(text(), 'yourText')]")

Is there a CSS selector for elements containing certain text?

Community
  • 1
  • 1
user3471881
  • 2,614
  • 3
  • 18
  • 34
1

Maybe using xpath language? Let's say you know object id, or other property it could be something like:

def select_option(element_text):
    xpath = '//select[@id=id]//option[contains(text(),' + element_text + ')]'
    driver.find_element_by_xpath(xpath)