Home > Software engineering >  Python Selenium's Key.RETURN is not returing new web page
Python Selenium's Key.RETURN is not returing new web page

Time:10-11

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

driver = webdriver.Firefox()
driver.implicitly_wait(40)
driver.get("https://www.youtube.com/")
elem = driver.find_element_by_name("search_query")
elem.clear()
elem.send_keys("python")
elem.send_keys(Keys.RETURN)

This code suppose to send "python" in the web page's search box and fetch a new web page but its not working and I have no idea why.

CodePudding user response:

You need to put some delay/wait before passing return key.

driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)

driver.get("https://www.youtube.com/")
elem = driver.find_element_by_name("search_query")
elem.clear()
elem.send_keys("python")
time.sleep(2)
elem.send_keys(Keys.RETURN)

CodePudding user response:

Another trick can be, when you enter text in the text box, try to simulate user action. for example, in this case, click on that text box and then enter.(refer to the code comment).

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

driver = webdriver.Firefox()
driver.implicitly_wait(40)
driver.get("https://www.youtube.com/")
elem = driver.find_element_by_name("search_query")
elem.clear()
elem.send_keys("python")
elem.click() # add this line to your code
elem.send_keys(Keys.RETURN)
  • Related