Reputation:
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?
Upvotes: 3
Views: 7468
Reputation: 1
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
Upvotes: 0
Reputation: 262
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")
Upvotes: 0
Reputation: 29372
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.
Upvotes: 2
Reputation: 1926
change
link_elements=driver.find_elements(By.CLASS,'BM30N')
to
link_elements=driver.find_elements(By.CLASS_NAME,'BM30N')
Upvotes: 1