How to find elements with two possible class names using an XPath
expression?
I'm working in Python with Selenium
and I want to find all elements which class
has one of two possible names.
'//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 |
.
You can use or
:
//div[@class="list"]/div[@class="item ng-scope highlight" or @class="item ng-scope"]//h3/a[@class="ng-binding"]
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"]
FYI, note how concise a CSS selector could be in your case:
div.list > div.item h3 a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With