Home > database >  How to get all elements with multiple classes in selenium
How to get all elements with multiple classes in selenium

Time:04-15

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?

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

CodePudding user response:

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

CodePudding user response:

Have you tried without spaces between class names?

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

CodePudding user response:

There is an undocumented function

driver.find_elements_by_css_selector(".foo.foo-default.cat.bar")
                   ^

This works.

  • Related