Felipe
Felipe

Reputation: 33

AttributeError when trying to use send_keys

I am trying to send_keys to a webelement but each time I do I get this error

driver.send_keys("admin")

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

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

import time

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver.get("http://gs.login.com")
cookies = driver.get_cookies()

driver.implicitly_wait(15)
driver.find_element_by_id('username')
driver.send_keys("admin")

time.sleep(10)
driver.quit()

I have look at documentation for selenium and I feel like have added the correct modules, but I still keep getting the same error message. Can someone please help.

Upvotes: 2

Views: 418

Answers (2)

JD2775
JD2775

Reputation: 3801

You need to store the element in a variable first, and then send_keys against that

element = driver.find_element_by_id('username')
element.send_keys("admin")

Upvotes: 1

John Gordon
John Gordon

Reputation: 33275

send_keys() is an element method, not a webdriver method.

When you call find_element_by_id(), you have to save the returned element and then you can call send_keys() on that element.

username_element = driver.find_element_by_id('username')
username_element.send_keys('admin')

Upvotes: 1

Related Questions