Python 和用于选择多个条件的 Selenium xpath

Python and Selenium xpath for selecting with multiple conditions

我在 selenium 中有以下代码,但仍然出现语法错误。我正在尝试 select 基于多个条件的元素。

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

感谢您提供的任何帮助。

根据 xpath 您分享如下:

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

您需要考虑几个事实:

  • 选择 <div> 标签的多个条件不能嵌套在 [] 内。您必须在一个 [] 内或多个 [] 内指定。
  • xpath 不应以不需要的字符结尾,例如 $

解决方案

您可以通过以下任一方式重写 xpath :

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and contains(@class, 'option')]")
# or
choices = driver.find_elements_by_xpath("//div[contains(.,'5')][contains(@class, 'option')]")