Struggling with Selenium - entering username and password

51 Views Asked by At

This is the code I have

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

driver = webdriver.Chrome()
driver.get("http://website.com")

element_user = driver.find_elements_by_id("user").send_keys("name123")

I keep getting this error

element_user = driver.find_elements_by_id("user").send_keys("")
AttributeError: 'list' object has no attribute 'send_keys'
3

There are 3 best solutions below

0
On

Find elements returns a list. Use find_element_by_id

0
On

find_elements_* returns a list, for a single WebElement use find_element_*. In addition, send_keys() doesn't have return statement so it returns default None. Split the command to two lines or remove the assignment

driver.find_element_by_id("user").send_keys("name123")
# or
element_user = driver.find_elements_by_id("user")
element_user.send_keys("name123")
0
On

You are using find_elements_by_id notice the s in elements.

DOCS (epmhasis mine)

Returns:

list of WebElement - a list with elements if any was found. An empty list if not

There's also a method find_element_by_id, that returns a single element if found.