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