用于选择多个条件的Python和Selenium xpath [英] Python and Selenium xpath for selecting with multiple conditions

查看:186
本文介绍了用于选择多个条件的Python和Selenium xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在selenium中具有以下代码,但继续出现语法错误.我正在尝试根据多种条件选择一个元素.

I have the following code in selenium but continue to get a syntax error. I'm trying to select an element based on multiple conditions.

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

感谢您提供的任何帮助.

Thanks for any help you can give.

推荐答案

根据您共享的 xpath ,如下所示:

As per the xpath you have shared as follows :

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

您需要考虑一些事实:

  • 选择<div>标签的多个条件不能在嵌套的[]中.您必须在一个[]内或在多个[]内指定.
  • xpath 不应以不需要的字符结尾,例如$
  • The multiple conditions for selecting the <div> tag can't be within nested []. Either you have to specify within one [] or within multiple []s.
  • The xpath shouldn't end with unwanted characters e.g $

您可以通过以下两种方式之一重写xpath:

You can rewrite the xpath in either of the following ways :

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

这篇关于用于选择多个条件的Python和Selenium xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆