how can i remove notifications and alerts from bro

2019-01-14 20:04发布

I am trying to submit information in a webpage, but selenium throws this error:

UnexpectedAlertPresentException: Alert Text: This page is asking you to confirm that you want to leave - data you have entered may not be saved. , >

It's not a leave notification; here is a pic of the notification -

enter image description here.

If I click in never show this notification again, my action doesn't get saved; is there a way to save it or disable all notifications?

edit: I'm using firefox.

4条回答
女痞
2楼-- · 2019-01-14 20:09

Usually with browser settings like this, any changes you make are going to get throws away the next time Selenium starts up a new browser instance.

Are you using a dedicated Firefox profile to run your selenium tests? If so, in that Firefox profile, set this setting to what you want and then close the browser. That should properly save it for its next use. You will need to tell Selenium to use this profile though, thats done by SetCapabilities when you start the driver session.

查看更多
甜甜的少女心
3楼-- · 2019-01-14 20:24

With the latest version of Firefox the above preferences didn't work.

Below is the solution which disable notifications using Firefox object

_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)

Disable notifications when using Remote Object: webdriver.Remote(desired_capabilities=_desired_caps, command_executor=_url, options=_custom_options, browser_profile=_browser_profile)

selenium==3.11.0

查看更多
Deceive 欺骗
4楼-- · 2019-01-14 20:28

This will do it:

from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(firefox_options=options)
查看更多
家丑人穷心不美
5楼-- · 2019-01-14 20:34

You can disable the browser notifications, using chrome options. Sample code below:

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)
查看更多
登录 后发表回答