Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find elements with two possible class names by XPath?

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.

  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 |.

like image 880
Milano Avatar asked Feb 11 '16 16:02

Milano


1 Answers

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
like image 78
alecxe Avatar answered Nov 09 '22 05:11

alecxe