如何使用Python和Selenium发送ESC键以关闭弹出窗口? [英] How to send ESC key to close pop up window using Python and Selenium?

查看:721
本文介绍了如何使用Python和Selenium发送ESC键以关闭弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上所述,有没有一种方法可以发送全局ESC键以关闭弹出窗口(CSS MODAL窗口)?我尝试了以下操作,但没有成功:

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)

我知道我可以使用xPath等,但是问题是该站点具有动态的elementId和类名.

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

推荐答案

您不需要将键发送到元素,您需要全局按一下(浏览器).

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()

您可以在 Webdriver API-7.2中查看更多信息.动作链文档

这篇关于如何使用Python和Selenium发送ESC键以关闭弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆