How can Selenium Chrome WebDriver notifications be handled in Python?
Have tried to dismiss/accept alert
and active element
but seems notif
Using Chrome Versão 85.0.4183.83 (Versão oficial) 64 bits and ChromeDriver 85.0.4183.83
Here is the code and it is working:
from selenium import webdriver
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)
driver.get("https://www.google.com/")
2020/08/27
The answer above from Dec '16 didn't work for me in August 2020. Both Selenium and Chrome have changed, I believe.
I'm using the binary for Chrome 84, which is listed as the current version, even though there's a binary for Chrome 85 available from selenium.dev
. I have to presume that's a beta since I have no other info on that.
I only have a partial answer so far, but I believe the following two lines are in the right direction. They're as far as I've gotten tonight. I have a test window that I've opened to Facebook and have the exact window shown in the Question. The code given in pythonjar's answer from Dec 30 '16 above produced error messages for me. These code lines worked without error, so I believe this is the right track.
>>> from selenium.webdriver import ChromeOptions
>>> chrome_options = ChromeOptions()
I'll update this tomorrow, if I get time to work on it.
Sorry for the partial answer. I hope to complete it soon. I hope this helps. If you get there first, please let me know.
You can disable the browser notifications, using chrome options.
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)
Follow the below code for handling the notification settings with Python selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")
option.add_experimental_option("prefs",
{"profile.default_content_setting_values.notifications": 2
})
Note 1-to allow notification, 2- to block notification