How to send ESC key to close pop up window using Python and Selenium?
Asked Answered
O

4

34

As mentioned, is there a way to send global ESC key to close popup(CSS MODAL Window)? I tried following but did not work:

driver.find_element_by_tag_name('body').send_keys(Keys.ESCAPE)

I know I can use xPath etc but issue is the site has dynamic elementIds and classnames.

Overcloud answered 14/1, 2017 at 12:20 Comment(3)
What kind of popup? Alert, window or frame?Tillandsia
@ElRuso Modal DivOvercloud
Related, see Typing Enter/Return key using Python and Selenium?Jiva
C
73

You don't need to send keys to the element, you need to press them globally (to browser).

You can do it via Actions.

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

webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()

You can see more info in Webdriver API - 7.2 Action Chains doc

Chockfull answered 16/4, 2017 at 12:53 Comment(3)
I had to add the import of ActionChains in order to get this to work, and just used ActionChains(driver).send_keys(Keys.ESCAPE).perform() without the webdriver bit in front. Import info here: https://mcmap.net/q/451318/-has-anyone-used-actionchains-of-webdriver-python-bindingAncestress
you need to import first thing you are going to work with - in this example it's either webdriver or ActionChainsChockfull
This works for me in desktop chrome, safari, firefox and edge, and in chrome on a samsung S23 emulator, but fails in Safari on iphone 14 with selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Error Domain=com.facebook.WebDriverAgent Code=1 "It is mandatory to have at least one gesture item defined for each action. Action with id 'mouse' contains none"Turbellarian
J
2

This code will send the "Esc" key in the browser window:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def sendesc(browser):
    ActionChains(browser).send_keys(Keys.ESCAPE).perform()
Janiecejanifer answered 28/4, 2022 at 7:28 Comment(2)
Could you add some human explanations of your solution, please ? that would be great : ) easier to understandThinia
@snoobdogg it initialize an action chain that will send the "Esc" key to the window of the browser, and then run the action chainJaniecejanifer
P
-2

I code my Selenium Python scripts in the AppRobotic Personal editor, and just insert its Windows macro functionality in between Selenium actions.

import win32com.client
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver

x.Type("{ESCAPE}")
Patroon answered 21/4, 2019 at 13:13 Comment(0)
A
-5

try also this it will go back to the previous driver u had

driver.back()
Aida answered 20/1, 2018 at 9:32 Comment(1)
this is different. based on documentation driver.back() goes one step backward in the browser history selenium-python.readthedocs.io/…Opening

© 2022 - 2024 — McMap. All rights reserved.