73

How can I filter elements that have the same class?

<html>
  <body>
    <p class="content">Link1.</p>
  </body>
</html>
<html>
  <body>
    <p class="content">Link2.</p>
  </body>
</html>
fdermishin
  • 3,267
  • 3
  • 20
  • 43
Sree
  • 739
  • 1
  • 5
  • 3
  • 7
    Is there anything (a code) you tried already? As you are new, please read our [Tour page](http://stackoverflow.com/tour) and especially [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – ZygD May 02 '15 at 12:33
  • 1
    Also you html code is not good formatted. You don't close tag. – Eugene May 02 '15 at 14:54
  • 1
    What are you going to do with the selected element? What criteria are you hoping to use? What have you already tried? – Richard May 03 '15 at 02:50

5 Answers5

88

You can try to get the list of all elements with class = "content" by using find_elements_by_class_name:

a = driver.find_elements_by_class_name("content")

Then you can click on the link that you are looking for.

LittlePanda
  • 2,438
  • 1
  • 20
  • 33
23

As per the HTML:

<html>
    <body>
    <p class="content">Link1.</p>
    </body>
<html>
<html>
    <body>
    <p class="content">Link2.</p>
    </body>
<html>

Two(2) <p> elements are having the same class content.

So to filter the elements having the same class i.e. content and create a list you can use either of the following Locator Strategies:

  • Using class_name:

    elements = driver.find_elements_by_class_name("content")
    
  • Using css_selector:

     elements = driver.find_elements_by_css_selector(".content")
    
  • Using xpath:

    elements = driver.find_elements_by_xpath("//*[@class='content']")
    

Ideally, to click on the element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CLASS_NAME:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "content")))
    
  • Using CSS_SELECTOR:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".content")))
    
  • Using XPATH:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@class='content']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
  • 1
    Neither class name nor css selector worked, no idea why, but xpath did, thanks a lot for that – Timeler Jan 14 '21 at 10:01
16

By.CLASS_NAME was not yet mentioned:

from selenium.webdriver.common.by import By

driver.find_element(By.CLASS_NAME, "content")

This is the list of attributes which can be used as locators in By:

CLASS_NAME
CSS_SELECTOR
ID
LINK_TEXT
NAME
PARTIAL_LINK_TEXT
TAG_NAME
XPATH

ZygD
  • 10,844
  • 36
  • 65
  • 84
13

Use nth-child, for example: http://www.w3schools.com/cssref/sel_nth-child.asp

driver.find_element(By.CSS_SELECTOR, 'p.content:nth-child(1)')

or http://www.w3schools.com/cssref/sel_firstchild.asp

driver.find_element(By.CSS_SELECTOR, 'p.content:first-child')
Stan E
  • 3,336
  • 17
  • 31
13

The most simple way is to use find_element_by_class_name('class_name')

wanderlust
  • 1,726
  • 1
  • 20
  • 23