3

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?

0

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

change

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

to

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

Comments

0

Java:

driver.findElement(By.className("input"));
&
driver.findElement(By.xpath("//input[@class='BM30N']"));

Python:

find_elements(By.CLASS_NAME, "BM30N")
&
driver.find_elements_by_class_name("BM30N")

Comments

0

Don't forget to import the "By" correctly, my VS Code only started to recognize the method after I imported it this way:

from selenium.webdriver.common.by import By

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.