1

Please could somebody tell me if it’s possible to quote 2 classes in an Xpath query? Please see below. The script errors when I run this. I am using python 2.7 via Selenium.

list_ppl_link =  driver.find_element_by_xpath('//div[@class="pro-tease"]//div[@class="pro-tease even"]')   
Lucas
  • 6,869
  • 5
  • 29
  • 44
Chris Moore
  • 49
  • 4
  • 8

2 Answers2

6

You should be able to select elements with multiple classes like so:

//div[contains(@class, "pro-tease") and contains(@class, "even")]

This will select divs that have both pro-tease and even class, e.g.

<div class="pro-tease something even">...</div>
<div class="irrelevant pro-tease foo even">...</div>

It will not, however, select divs that have only one of the two specified classes. If you want to select an element that has either one class or the other, try this:

//div[contains(@class, "pro-tease") or contains(@class, "even")]

Now, if you had four divs:

  1. <div class="pro-tease irrelevant">...</div>
  2. <div class="even irrelevant">...</div>
  3. <div class="not specified">...</div>
  4. <div class="pro-tease even">...</div>

1, 2, 4 would be selected, as they all contain either pro-tease or even.

By this point, you probably noticed that contains() does exactly what it says: checks if the class name contains the specified argument. Hence, if you join two contains() with and, you are basically saying "give me elements that contain this class and also this one", whereas with or it's more like "give me elements that have either this class, or this class".

By the way, you can test your queries in Chrome developer mode (Ctrl+Shift+I) by searching the elements (Ctrl + F). If your query does not work in the browser, it will probably not work in elsewhere either.

cegas
  • 2,823
  • 3
  • 16
  • 16
  • Hi, when I used the following bit of code: list_ppl_link = driver.find_elements_by_xpath('//div[contains(@class, "pro-tease") and contains(@class, "even")]') ....it only gave me half the records available and only brought back the class of "pro-tease even". Do you no why it didnt also get the "pro-tease" class? – Chris Moore Feb 15 '17 at 10:31
  • Basically on the website there are rows that contain the class "pro-tease" and other rows that contain the class "pro-tease even". I want to pull of all of the links at the same time – Chris Moore Feb 15 '17 at 10:42
  • I have updated the answer - my understanding was that you wanted *both* classes to be present. If you want either one or the other to be present, use `or` instead of `and`, like so: `//div[contains(@class, "pro-tease") or contains(@class, "even")]`. – cegas Feb 15 '17 at 14:29
  • Thanks very much for your help – Chris Moore Feb 15 '17 at 15:18
  • Happy to help! If it solved your issue, I would appreciate if you could [accept it](http://stackoverflow.com/help/someone-answers). – cegas Feb 15 '17 at 15:27
0

See How to get html elements with multiple css classes

What you're looking for is //div[contains(@class, 'class1') and contains(@class, 'class2')]

Community
  • 1
  • 1
Dillanm
  • 876
  • 12
  • 28