How to send ESC key to close pop up window using Python and Selenium? How to send ESC key to close pop up window using Python and Selenium? selenium selenium

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


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 webdriverfrom selenium.webdriver.common.keys import Keyswebdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()

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


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

import win32com.clientx = win32com.client.Dispatch("AppRobotic.API")from selenium import webdriverx.Type("{ESCAPE}")


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

driver.back()