Sree
Sree

Reputation: 939

Finding elements by class name with Selenium in Python

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>

Upvotes: 93

Views: 484509

Answers (8)

Mark
Mark

Reputation: 1

In Python, Selenium provides a method called find_elements_by_class_name to find elements with the same class name. This method returns a list of all elements that match the provided class name.

Let's say you're trying to find all paragraphs (

) with the class "content". Here's how you can do it:

from selenium import webdriver

initialize the driver

driver = webdriver.Firefox() # replace with the browser driver of your choice

navigate to your webpage

driver.get('https://www.softwaretestingsapiens.com/finding-elements-by-class-name-with-selenium-in-python/')

find elements by class name

elements = driver.find_elements_by_class_name('content')

for el in elements: print(el.text)

This script will print the text inside each

tag with the class "content". In your case, it should print "Link1." and "Link2.".

Remember, Selenium requires a specific driver to interface with the chosen browser. Firefox requires geckodriver, which needs to be installed before the above script can be run.

Also, keep in mind that find_elements_by_class_name returns a list, even if there's only one match. If you're certain there's only one element of interest, use find_element_by_class_name (note the lack of 's' in 'element') to get the WebElement directly.

Lastly, it's recommended to add some error handling to your script to manage situations where the elements are not found. This will make your code more robust and easier to debug.

Happy coding!

Upvotes: -1

FabricioG
FabricioG

Reputation: 3320

The question was related to "elements" plural, most are for element. To wait for the element and then iterate over each:

selectableEls = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "content")))
for el in selectableEls:
    print(el)

Upvotes: 0

JackDonMcLovin
JackDonMcLovin

Reputation: 177

The first answer has been deprecated, and the other answers only return one result. This is the correct answer:

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

Upvotes: 5

undetected Selenium
undetected Selenium

Reputation: 193298

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:

Upvotes: 33

LittlePanda
LittlePanda

Reputation: 2507

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.

Upvotes: 89

ZygD
ZygD

Reputation: 24478

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

Upvotes: 60

wanderlust
wanderlust

Reputation: 1936

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

Upvotes: 12

Stan
Stan

Reputation: 3461

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')

Upvotes: 14

Related Questions