Home > database >  How do I find element by class name in selenium?
How do I find element by class name in selenium?

Time:04-25

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?

CodePudding user response:

change

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

to

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

CodePudding user response:

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.

  • Related