【问题标题】:Selenium's find_elements with two clauses for text?Selenium 的 find_elements 有两个文本子句?
【发布时间】:2021-01-11 00:24:13
【问题描述】:

我如何根据文本使用 selenium 的 find_elements_by_xpath(),它可能有词也可能没有词?

例如:它可以是#1 here#1 is here。我希望两者都属于同一个列表,因为该 func 返回一个列表。 ATM 我有driver.find_elements_by_xpath("//*[contains(text(), '#1 here')]"),但这只会找到第一种情况,而不是is 的情况。基本上,类似driver.find_elements_by_xpath("//*[contains(text(), '#1 here' or '#1 is here')]")

我该怎么做?

我也在努力保持他们的“秩序” 所以,从上到下的第一个是列表中的第一名,等等

我可以做 2 个不同的列表,每个列表一个,然后将它们组合起来,但这也会搞砸

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping xpath


    【解决方案1】:

    您可以使用or 子句,您可以使用以下基于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')]")
      

    【讨论】:

    • 您的 xpath 中有错字。我想你的意思是contains(., 'or this') 而不是contains(,. 'or this'),对吧?
    猜你喜欢
    • 2017-03-20
    • 2023-02-01
    • 2016-06-15
    • 2021-10-13
    • 2019-09-09
    • 2019-11-26
    • 1970-01-01
    • 2022-01-24
    • 2012-08-28
    相关资源
    最近更新 更多