How to Find Elements by Class

How to correctly find elements by class name on Python? Selenium related

You are using this CLASS_NAME elements__StyledListItem-sc-197zmwo-0 QbTKh which has space in it.

In Selenium, a class name having space will not be parsed and will throw the error.

The reason why you did not get the error is cause you are using find_elements that will either return a list of web element or nothing.

So how to resolve this?

remove the space and put a . instead to make a CSS_SELECTOR

try this:

wallet_providers = driver.find_elements(By.CSS_SELECTOR, ".elements__StyledListItem-sc-197zmwo-0.QbTKh") #get the list of wallet providers

to be honest we can have better locator than this, cause it seems these values 197zmwo-0.QbTKh are generated dynamically.

I would rather use this CSS:

li[class^='elements__StyledListItem'] span[font-weight]

or this xpath:

//li[starts-with(@class,'elements__StyledListItem')]//descendant::span[@font-weight]

Also, you should print it like this: (this is one way but there are others as well):

Code:

driver.get("https://opensea.io/")

WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
wallet_providers = driver.find_elements(By.CSS_SELECTOR, "li[class^='elements__StyledListItem'] span[font-weight]") #get the list of wallet providers
for i in wallet_providers:
print(i.get_attribute('innerText'))

Console output:

WalletConnect
MetaMask
Coinbase Wallet
Fortmatic

Process finished with exit code 0

How to find elements by class

You can refine your search to only find those divs with a given class using BS3:

mydivs = soup.find_all("div", {"class": "stylelistrow"})

How do I find element by class name in selenium?

You must be using Selenium 4.

In Selenium4

find_elements_by_class_name

and other find_elements_by_** have been deprecated.

You should use find_element(By.CLASS_NAME, "") instead

So your effective code would be:

link_elements = find_elements(By.CLASS_NAME, "BM30N")

this should help you past the issue.

How to find elements by class name length

i'm just answering my own question because i think i found a very fast method to do it

i wanted to find classname "g" or "g <6alphanumerics>"
for some reason in javascript if you make document.getElementsByClassName('g') it finds everything i needed

So the new snippet is:

classes = x.execute_script("return document.getElementsByClassName('g')")

the return is important otherwise the array will be empty

How to find elements that are either to one or another class attached? Selenium Pyton

To create a list of elements with the value of class attribute either as classA or classB you can use either of the following Locator Strategies:

  • Using css_selector:

    buttons = self.driver.find_elements(By.CSS_SELECTOR, "button.classA, button.classB")
  • Using xpath:

    buttons = self.driver.find_elements(By.XPATH, "//button[@class='classA' or @class='classB']")

Selenium find multiple elements by class name

Since your css_selector is based on the class name, your expression should be:

divs = driver.find_elements_by_css_selector('.iwantthis')

or

divs = driver.find_elements_by_css_selector('a.iwantthis')

So you just missing a dot . in your css_selector.

You can also try the following XPath:

divs = driver.find_elements_by_xpath('//a[contains(@class,'iwantthis')])

Selenium Not Able to Find Elements By ClassName

This is doc from By.CLASS_NAME found in selenium-java-bindings.

Find elements based on the value of the "class" attribute. Only one class name should be used. If an element has multiple classes, please use cssSelector(String).

Try with (By.CSS_SELECTOR, '.sc-fAEnHe.ePMtc'):

try:
match_history_table = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.sc-fAEnHe.ePMtc'))
)
except Exception:
print("Error Finding Match History Table")
driver.quit()


Referenses

  • json wire protocol
    https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelement

class name Returns an element whose class name contains the search value; compound class names are not permitted.

  • W3C webdriver

    And class_name not mentioned in W3C protocol, so it might become legacy
    https://www.w3.org/TR/webdriver/#locator-strategies

Selenium find element by class name doesn't work

The element with this class name does not exist, so the class name is wrong. I think because of the whitespace at the class name.
Try this:

element = driver.find_element_by_class_name("small-widget.get-started-widget")


Related Topics



Leave a reply



Submit