How to Fix AttributeError: ‘list’ object has no attribute ‘send_keys’

AttributeError: ‘list’ object has no attribute ‘send_keys’ error occurs when you accidently use the send_keys method on a list object instead of a Selenium WebElement object.

Visual Representation of the error and its solution

Pictorial representation of AttributeError - 'list' object has no attribute 'send_keys'

Incorrect way

elements = driver.find_elements_by_id("some_id")

elements.send_keys("some text") 

This will cause the AttributeError.

Correct way

# Correct way

element = driver.find_element_by_id("some_id") # Note: singular 'element'

element.send_keys("some text")

If you are sure there are multiple elements, and you want to send keys to all of them, you would need to loop through them:

elements = driver.find_elements_by_id("some_id")

for element in elements:
  element.send_keys("some text")

Always check if you are working with a list or a single web element before using methods like send_keys.

Another solution

For better flexibility, you can also use the find_elements_by_xpath() method.

Using driver.find_elements_by_xpath(“some_id”) will return a list of WebElement objects that match the given XPath, even if there’s only one match.

If you are trying to interact with a single element, you should use the driver.find_element_by_xpath(“some_id”) (note the singular “element” in the method name). This will return the first WebElement that matches the given XPath.

That’s it!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.