本文介绍了Selenium 的 find_elements 带有两个文本子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 selenium 的 find_elements_by_xpath() 基于文本,当它可能有也可能没有词时?

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

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

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')]")

我怎么能这样做?

我也在努力保持他们的秩序";所以,从上到下的第一个是列表中的#1,等等

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

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

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

推荐答案

您可以使用 or 子句,您可以使用以下任一 xpath 基于 定位器策略:

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')]")
    

  • 这篇关于Selenium 的 find_elements 带有两个文本子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    11-01 01:11