如何使用Selenium Webdriver单击"显示通知"弹出窗口中的"允许"

Sid*_*ant 35 selenium selenium-webdriver

我正在尝试登录Facebook.登录成功后,我会弹出一个浏览器:

显示通知

如何使用webdriver我可以单击"允许"并继续前进?

Pri*_*ske 36

请按照以下步骤操作:

步骤1:

//创建ChromeOptions类的实例

//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();

//Add chrome switch to disable notification - "**--disable-notifications**"
options.addArguments("--disable-notifications");

//Set path for driver exe 
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");

//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)

第2步:

//添加chrome开关以禁用通知 - " - disable -notifications "

//Create a map to store  preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//add key and value to map as follow to switch off browser notification
//Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create an instance of ChromeOptions 
ChromeOptions options = new ChromeOptions();

// set ExperimentalOption - prefs 
options.setExperimentalOption("prefs", prefs);

//Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)

第3步:

//设置驱动程序exe的路径

    WebDriver driver ;
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("permissions.default.desktop-notification", 1);
    DesiredCapabilities capabilities=DesiredCapabilities.firefox();
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    driver = new FirefoxDriver(capabilities);
    driver.get("http://google.com");
Run Code Online (Sandbox Code Playgroud)

第4步 :

//将ChromeOptions实例传递给ChromeDriver构造函数

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")

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 1 
})

driver = webdriver.Chrome(chrome_options=option, executable_path='path-of- 
driver\chromedriver.exe')
driver.get('https://www.facebook.com')
Run Code Online (Sandbox Code Playgroud)

  • 这适用于旧版chromedriver。(与Chrome 49兼容的版本...)此功能不适用于更高版本。不知道截止时间是什么,但是对于最新版本,您需要使用prefs HashMap选项。索拉(Saurabh Gaur)的答案。 (2认同)

Sau*_*aur 9

这不是警告框,因此您无法使用它Alert,这是一个Chrome浏览器通知,要关闭此浏览器通知,您需要使用chrome选项创建chrome首选项地图,如下所示: -

//Create prefs map to store all preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//Put this into prefs map to switch off browser notification
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create chrome options to set this prefs
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);

//Now initialize chrome driver with chrome options which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);

//Now do your further steps
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..:)


小智 7

import unittest

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time

class SendMsgSkype(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

        options = Options()
        options.add_argument("--disable-notifications")

        cls.driver = webdriver.Chrome('./chromedriver.exe', chrome_options=options)

        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()
        cls.driver.get("https://web.skype.com/ru/")
Run Code Online (Sandbox Code Playgroud)

这个对我有用.更多详情请访问:http://nullege.com/codes/show/src@t@a@TardyParty-HEAD@oxbiz.py/21/selenium.webdriver.Chrome


SIM*_*SIM 6

到目前为止,我遇到的唯一可行的解​​决方案是:

from selenium.webdriver.chrome.options import 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)
Run Code Online (Sandbox Code Playgroud)