Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get all elements with multiple classes in selenium

This is how I get the website

from selenium import webdriver
url = '...'
driver = webdriver.Firefox()
driver.get(url)

Now I want to extract all elements with a certain classes into a list

<li class=foo foo-default cat bar/>

How would I get all the elements from the website with these classes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is something like

fruit = driver.find_element_by_css_selector("#fruits .tomatoes")

But when I do this (I tried without spaces between the selectors too)

elements = driver.find_element_by_css_selector(".foo .foo-default .cat .bar")

I get

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .foo .foo-default .cat .bar
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

These are the classes I copied from the DOM`s website though…

>Solution :

If this is just the HTML

<li class=foo foo-default cat bar/>

You can remove the space and put a . to make a CSS SELECTOR as a locator.

elements = driver.find_elements(By.CSS_SELECTOR, "li.foo.foo-default.cat.bar")
print(len(elements))

or my recommendation would be to use it with explicit waits:

elements_using_ec = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "li.foo.foo-default.cat.bar")))
print(len(elements))

Imports:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading