[英]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.