Selenium's find_elements with two clauses for text?

本小妞迷上赌 提交于 2021-01-28 12:14:04

问题


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


回答1:


You can use the or clause you can use either of the following xpath 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')]")
    


来源:https://stackoverflow.com/questions/65659942/seleniums-find-elements-with-two-clauses-for-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!