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

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

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?

>Solution :

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.

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