简体   繁体   English

如何在不使用 alert.accept() 的情况下使用 Selenium 在 Brave/Chrome 中禁用弹出窗口/警报?

[英]How to disable popups/alerts in Brave/Chrome using Selenium without using alert.accept()?

I can't use alert.accept() since my program will automatically defocus before it can work (it has to check for other things as well).我不能使用alert.accept()因为我的程序会在它工作之前自动散焦(它还必须检查其他事情)。

I don't want any popups at all, but I don't know how to outright disable them, which is important since they prevent me from closing the tab.我根本不想要任何弹出窗口,但我不知道如何彻底禁用它们,这很重要,因为它们阻止我关闭选项卡。 I haven't been able to find any answers that don't involve alert.accept() and use python.我找不到任何不涉及alert.accept()并使用 python 的答案。

Can I do this?我可以这样做吗? If not, are there any workarounds for my situation?如果没有,是否有针对我的情况的解决方法? (If there aren't the program I'm trying to make is impossible.) (如果没有我想做的程序是不可能的。)

As this question says, as long as you're okay blocking all alerts, you can just override alert() .正如这个问题所说,只要您可以阻止所有警报,您就可以覆盖alert() Here's an example in Python:这是 Python 中的示例:

from selenium import webdriver

driver=webdriver.Firefox()
driver.implicitly_wait(3)
driver.get("http://example.com")
js = "window.alert = function() {}"
driver.execute_script(js)

Or because the alert pops up on page close, you could try this:或者因为在页面关闭时弹出警报,你可以试试这个:

from selenium import webdriver

driver=webdriver.Firefox()
driver.implicitly_wait(3)
driver.get("http://example.com")
js = "window.onbeforeunload = null"
driver.execute_script(js)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多次使用alert.accept()不能正常运行 - Using alert.accept() multiple times does not function properly 如何使用硒在弹出窗口中接受警报? - How to accept alert in the popup window using selenium? 在python中使用selenium时如何处理google chrome确认提示? - How to deal with google chrome confirmation alerts when using selenium in python? 如何使用勇敢的浏览器使用 python 启动 selenium web 驱动程序 - How to initiate selenium web driver with brave browser using python 如何在 Windows 上使用 Selenium 和 Python 启动 Brave 浏览器 - How to initiate Brave browser using Selenium and Python on Windows 如何使用selenium处理定时弹出窗口? - How to handle timed popups using selenium? 弃用警告:在 Python Selenium 和 Windows 上使用 Brave 浏览器时使用选项而不是 chrome_options 错误 - DeprecationWarning: use options instead of chrome_options error using Brave Browser With Python Selenium and Chromedriver on Windows 如何使用 selenium + chrome webdriver + python 忽略警报? - How do I ignore an alert using selenium + chrome webdriver + python? 使用 selenium 在 Chrome 中接受对麦克风的请求 - Accept request to microphone in Chrome using selenium 使用 selenium 在 chrome 中接受剪贴板权限请求 - Accept Clipboard permission request in chrome using selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM