The way I know of sending keys using the WebDriver Python bindings is as follows:


How can I send a key (specifically the Escape key) to no element in particular, just to the window in general?

  • You can send the keys to the body of the html page. Or do you mean you want to send keys to the browser itself, and not to something rendered on the page?
    – Dale Emery
    Jan 20, 2012 at 22:39
  • "specifically the Escape key" Are you attempting to dismiss/close a pop-up dialog perhaps? Jan 21, 2012 at 5:12
  • Yes, I'm attempting to close a dialog.
    – Randomblue
    Jan 21, 2012 at 11:30

3 Answers 3


How do I handle pop up windows?

WebDriver offers the ability to cope with multiple windows. This is done by using the "WebDriver.switchTo().window()" method to switch to a window with a known name. If the name is not known, you can use "WebDriver.getWindowHandles()" to obtain a list of known windows. You may pass the handle to "switchTo().window()".

  • This does not answer the question "Send keys to no element in particular".
    – dotancohen
    Aug 23, 2022 at 13:55

Try setting the element to the body of the HTML document:

element = driver.find_element_by_xpath("//body")
  • Could you please edit your answer to explain what you are adding to the accepted answer? When adding an answer to a question this old that has an accepted answer, a simple one-liner is likely to be considered inadequate.
    – Kate Paulk
    Feb 24, 2020 at 15:29
  • I added an example. Even though there is an accepted answer (and the asker obviously is happy with it), I think this answers the original post more directly.
    – Mate Mrše
    Feb 25, 2020 at 7:53
  • 1
    This worked for me. Thanks for adding this answer!
    – dfrankow
    Apr 15, 2021 at 15:39

The proper way to send keys to the window without specifying any particular element in Selenium is with the ActionChains.

In Python, it looks like this:

from selenium.webdriver.common.action_chains import ActionChains

value = "Randomblue"

actions = ActionChains(driver)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.