如何通过 XPath 查找具有两个可能的类名的元素? [英] How to find elements with two possible class names by XPath?

查看:25
本文介绍了如何通过 XPath 查找具有两个可能的类名的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 XPath 表达式查找具有两个可能的类名的元素?

How to find elements with two possible class names using an XPath expression?

我正在使用 Selenium 使用 Python,我想找到 class 具有两个可能名称之一的所有元素.

I'm working in Python with Selenium and I want to find all elements which class has one of two possible names.

  1. class="item ng-scope highlight"
  2. class="item ng-scope"

'//div[@class="list"]/div[@class="item ng-scope highlight"]//h3/a[@class="ng-binding"]'

当然,我可以进行两次单独的搜索并将结果合并到一个列表中.但是还有一种更简单有效的方法.也许通过使用 |.

Of course I can do two separate searches and concat results into one list. But there is a more simple and efficient way. Maybe by using |.

推荐答案

您可以使用:

//div[@class="list"]/div[@class="item ng-scope highlight" or @class="item ng-scope"]//h3/a[@class="ng-binding"]

请注意,ng-scope 通常不是一个好的类名,因为它是一个纯技术"的 AngularJS 特定类(ng-binding 实际上)角度元素具有的.请查看使用 contains() 并检查 item 类是否足以涵盖用例:

Note that ng-scope in general is not a good class name to rely on, because it is a "pure technical" AngularJS specific class (same goes for the ng-binding actually) that angular elements have. Please see if using contains() and checking the item class only would be enough to cover the use case:

//div[@class="list"]/div[contains(@class, "item")]//h3/a[@class="ng-binding"]

仅供参考,请注意 CSS 选择器在您的情况下有多简洁:

FYI, note how concise a CSS selector could be in your case:

div.list > div.item h3 a

这篇关于如何通过 XPath 查找具有两个可能的类名的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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