Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从浏览器中删除通知和警报?selenium python 2.7.7_Python_Python 2.7_Selenium_Selenium Webdriver_Selenium Firefoxdriver - Fatal编程技术网

如何从浏览器中删除通知和警报?selenium python 2.7.7

如何从浏览器中删除通知和警报?selenium python 2.7.7,python,python-2.7,selenium,selenium-webdriver,selenium-firefoxdriver,Python,Python 2.7,Selenium,Selenium Webdriver,Selenium Firefoxdriver,我试图在网页中提交信息,但selenium抛出以下错误: UnexpectedAlertPresentException:警报文本:此页面正在询问您 要确认您想离开-您输入的数据可能不正确 得救了, > 这不是请假通知;以下是通知的图片- 如果我单击“不再显示此通知”,我的操作将不会保存;有没有办法保存或禁用所有通知 编辑:我使用的是firefox。通常在这样的浏览器设置下,Selenium下次启动新的浏览器实例时,您所做的任何更改都会被丢弃 您是否使用专用的Firefox配置文件来运行sel

我试图在网页中提交信息,但selenium抛出以下错误:

UnexpectedAlertPresentException:警报文本:此页面正在询问您 要确认您想离开-您输入的数据可能不正确 得救了, >

这不是请假通知;以下是通知的图片-

如果我单击“不再显示此通知”,我的操作将不会保存;有没有办法保存或禁用所有通知


编辑:我使用的是firefox。

通常在这样的浏览器设置下,Selenium下次启动新的浏览器实例时,您所做的任何更改都会被丢弃


您是否使用专用的Firefox配置文件来运行selenium测试?如果是这样,在Firefox配置文件中,将此设置设置为所需,然后关闭浏览器。这应该会妥善保存它,以备下次使用。不过,您需要告诉Selenium使用此配置文件,这是在启动驱动程序会话时由SetCapabilities完成的

您可以使用chrome选项禁用浏览器通知。示例代码如下:

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

在最新版本的Firefox中,上述首选项不起作用

下面是使用Firefox对象禁用通知的解决方案

_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)
使用远程对象时禁用通知:
webdriver.Remote(所需功能=\u所需上限,命令执行器=\u url,选项=\u自定义选项,浏览器配置文件=\u浏览器配置文件)

硒==3.11.0

这可以做到:

from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(firefox_options=options)

这个答案是基于Chrome(80.0.3987.163版)的代码片段的改进

lk=os.path.join(os.getcwd(),“chromedriver”,)-->在这一行中,您提供了指向chromedriver的链接,您可以从


对于Google Chrome和Selenium v3,您可能会收到“弃用警告:使用选项而不是Chrome_选项”,因此您需要执行以下操作:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

注意:我使用的是webdriver管理器,但这也适用于指定可执行文件路径。

是的,我使用firefox。配置文件管理内置于firefox中,从运行命令启动firefox,名为“firefox-p”,进入管理器。我通常有一个专用的Webdriver实例。我还将在其中安装Firebug,以简化调试。网上有很多关于stackoverflow设置功能的代码示例(我就是这样学习的)。对于我来说,当加载一个新页面时,我想要的元素是不可通过add访问的。在实现您的解决方案后,添加仍然被加载,但被scraper忽略,并且我不再得到错误“点(xxx)处的元素不可读取”这并不能解决Chrome 74中我的“您确定要离开页面吗”弹出窗口的问题。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)