0

Whether this is opening a tab, saving a bookmark, printing a file or whatever, Selenium can't register key presses. I've tried the following approaches:

1. driver.find_element_by_tag_name("body").send_keys([insert key here])
2. ActionChains(driver).send_keys([insert key here]).perform()
3. ActionChains(driver).key_down(Keys.CONTROL).send_keys([insert key here]).key_up(Keys.CONTROL).perform()

I've also tried putting driver.find_element_by_tag_name('body').click() in front of the each of those lines to force the browser to focus on the page, but even this doesn't work.

THank you in advance for your help.

  • 1
    They've pretty much limited actions to the DOM (handlers in the web page). I think some of these shortcuts used to work in older versions of driver/browser, but not so much these days. Though one that does still work is paste: element.sendKeys(Keys.CONTROL , "v"); – pcalkins Jan 09 '21 at 00:19

1 Answers1

0
driver = webdriver.Firefox()
driver.get("https://www.google.com")
input= driver.find_element_by_xpath('//input[@title="Search"]')
input.send_keys("hi this is a test")
input.send_keys(Keys.CONTROL+"a")
input.send_keys(Keys.CONTROL+"x")
time.sleep(5)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
time.sleep(5)

This code types something on the google search field , selects all, cuts the text and again paste it back .

It works fine , please add the code you tried and the website where you are trying it

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • This doesn't work. I'm using Tor Browser and want to create a new Circuit (Ctrl+Shift+L). The code crashes if I try to "send_keys" to an image, but does nothing if I send it to the body or a search bar. For example: `driver.get('https://duckduckgo.com/')` `bar = driver.find_element_by_xpath('//*[@id="search_form_input_homepage"]')` `bar.send_keys(Keys.CONTROL+Keys.SHIFT+"L")` Does nothing. //*[@id="search_form_input_homepage"] is DuckDuckGo's search bar by the way, just as //input[@title="Search"] is Google's. – user14892837 Jan 10 '21 at 12:43