plx23
plx23

Reputation: 89

Struggling with Selenium - entering username and password

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'

Upvotes: 0

Views: 48

Answers (3)

Guy
Guy

Reputation: 50949

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")

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

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.

Upvotes: 2

Anger Density
Anger Density

Reputation: 332

Find elements returns a list. Use find_element_by_id

Upvotes: 1

Related Questions