Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find element by class name in selenium?

What is the syntax for finding element by class name in selenium? please be aware that I have already used the syntax:

link_elements = driver.find_elements_by_class_name("BM30N")

and it gives me the following error:

C:\Users\David\Desktop\Selenium\Crawl.py:17: DeprecationWarning: find_elements_by_class_name is deprecated. Please use find_elements(by=By.CLASS_NAME, value=name) instead
  link_elements = driver.find_elements_by_class_name("BM30N")

When I use:

link_elements=driver.find_elements(By.CLASS,'BM30N')

I get:

AttributeError: type object 'By' has no attribute 'CLASS'

But the above syntax works perfectly fine for ID and NAME:

link_elements=driver.find_elements(By.NAME,'product-item')
link_elements=driver.find_elements(By.ID,'product-item')

Any ideas as to what the correct syntax should be for searching by class?


2 Answers

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.

like image 157
cruisepandey Avatar answered Nov 29 '25 18:11

cruisepandey


change

link_elements=driver.find_elements(By.CLASS,'BM30N')

to

link_elements=driver.find_elements(By.CLASS_NAME,'BM30N')
like image 22
Akzy Avatar answered Nov 29 '25 16:11

Akzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!