Programming Maniac
Programming Maniac

Reputation: 127

(Selenium) send_keys(Keys.ENTER) not working but but showing error

The code below is not working. When I execute the code, its runs perfectly except this line:

driver.find_element_by_name("btnK").send_keys(Keys.ENTER)

Even though the line doesn't run it still doesn't give an error. The error is that the line of code doesn't click the button with the name, 'btnK'.

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

driver = webdriver.Chrome()  

driver.set_page_load_timeout(10)  
driver.get("https://www.google.com")  
driver.find_element_by_name('q').send_keys("Automation Step by Step")  
time.sleep(1)  
driver.find_element_by_name("btnK").send_keys(Keys.ENTER)  
driver.maximize_window()  
time.sleep(2)  
driver.quit()  

Upvotes: 3

Views: 2944

Answers (1)

Xion
Xion

Reputation: 374

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

driver = webdriver.Chrome()

driver.set_page_load_timeout(10)
driver.get("https://www.google.com")
driver.find_element_by_name('q').send_keys("Automation Step by Step")
time.sleep(1)
driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div/div[3]/center/input[1]").send_keys(Keys.ENTER)
time.sleep(10)
driver.quit()

I manage to fix it by changing the xpath for you.

Apparently there is another element by name class that has btnK but does not seem to link to anything, so sending key to that first element by name just does nothing.

Upvotes: 4

Related Questions