Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 418
Volatil3 Avatar asked Jan 14 '17 12:01

Volatil3


People also ask

How do I stop pop ups in Selenium Python?

To dismiss a popup, the method switch_to. alert(). dismiss() is used. To obtain the text on a popup, we have to use the switch_to.


1 Answers

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

like image 153
MrHant Avatar answered Oct 24 '22 00:10

MrHant