NotMyName
NotMyName

Reputation: 85

send_keys(Keys.RETURN) is throwing an error

I'm new to Selenium(Python). I'm trying to get the program to open google.com and type in a piece of text to be searched. Everything is fine, except it doesn't click Enter to search.

Here's the source code

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

driver = webdriver.Chrome('C:\Anaconda3\Lib\idlelib\chromedriver.exe')

driver.set_page_load_timeout("10")
driver.get("http://google.com")
driver.find_element_by_name("q").send_keys(//The piece of text)
driver.find_element_by_name("btnK").send_key(Keys.RETURN)
time.sleep(4)

And it is throwing me this error AttributeError: 'WebElement' object has no attribute 'send_key'

Can anyone tell me where I'm wrong and what I should do to fix it?

Upvotes: 2

Views: 1282

Answers (1)

SeleniumUser
SeleniumUser

Reputation: 4177

If you want to press enter key then follow below solution.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe")


driver.set_page_load_timeout("10")
driver.get("http://google.com")
element=driver.find_element_by_name("q")
element.send_keys("selenium")
# element=driver.find_element_by_name("btnK")
element.send_keys(Keys.RETURN);

Upvotes: 1

Related Questions