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

Noa*_*iff 5 selenium xpath python-3.x selenium-webdriver

我在硒中有以下代码,但继续出现语法错误。我试图根据多个条件选择一个元素。

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

谢谢你提供的所有帮助。

Deb*_*anB 5

按照您共享的xpath如下:

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

您需要考虑一些事实:

  • 选择<div>标记的多个条件不能在嵌套中[]。您必须在1个[]或多个内指定[]
  • 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')]")
Run Code Online (Sandbox Code Playgroud)