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

AttributeError when trying to use 'find_element_by_class_name' with Selenium in Python

I’m trying to create a tool that auto-types each letter from a specific class on a webpage using Selenium WebDriver. However, I’m encountering an error:

AttributeError: 'WebDriver' object has no attribute 'find_elements_by_class_name'

Here is the Python code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Edge()

driver.get("https://example.com")

letters_divs = driver.find_elements_by_class_name("word")

for div in letters_divs:
    letters = div.find_elements_by_tag_name("letter")
    
    for letter in letters:
        target_element = driver.find_element_by_id("wordsInput")
        target_element.send_keys(letter.text)
driver.quit()

The error occurs at the line where I’m trying to find the elements by class name. I have the class name and id of the text field where the typing should occur.

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

Any help on why this error is occurring and how to fix it would be greatly appreciated.

>Solution :

If you are using a recent version of Selenium, the find element(s) methods in the form of .find_elements_by_* have been deprecated and removed. Replace them with

driver.find_elements(By.CLASS_NAME, "word")
driver.find_elements(By.TAG_NAME, "letter")
driver.find_elements(By.ID, "wordsInput")

and so on. Please refer to the docs for more info.

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