1

How can I use selenium's find_elements_by_xpath() based on text, when it may or may not have a word?

Example: it can be either #1 here or #1 is here. I want both to be part of the same list, since that func return a list. ATM I have driver.find_elements_by_xpath("//*[contains(text(), '#1 here')]") but that would only find the first case, not the ones with an is. Basically, something like driver.find_elements_by_xpath("//*[contains(text(), '#1 here' or '#1 is here')]")

How could I do that?

I'm also trying to keep their "order" So, first one from top to bot is #1 on list, etc

I could do 2 different lists, one for each and then combine them but that would also screw that up

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dvnt
  • 196
  • 4
  • 15

1 Answers1

3

You can use the or clause you can use either of the following based Locator Strategies:

  • Selenium3:

    driver.find_elements_by_xpath("//*[contains(., 'this') or contains(., 'or this')]")
    
  • Selenium4:

    driver.find_elements(By.XPATH, "//*[contains(., 'this') or contains(., 'or this')]")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    There is a typo in your xpaths. I suppose you meant `contains(., 'or this')` instead of `contains(,. 'or this')`, right? – SIM Jan 11 '21 at 07:25