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

如何使用Selenium Webdriver单击允许显示通知弹出窗口,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我正在尝试登录Facebook。成功登录后,我会看到一个浏览器弹出窗口: 如何使用webdriver单击“允许”并继续?这不是一个警报框,因此您无法使用警报处理它,这是一个chrome浏览器通知,要关闭此浏览器通知,您需要使用chrome选项创建chrome首选项图,如下所示:- try { // Check the presence of alert Alert alert = driver.SwitchTo().Alert(); // if present consu

我正在尝试登录Facebook。成功登录后,我会看到一个浏览器弹出窗口:


如何使用webdriver单击“允许”并继续?

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

try {

   // Check the presence of alert
   Alert alert = driver.SwitchTo().Alert();

   // if present consume the alert
   alert.Accept();

  } catch (NoAlertPresentException ex) {
     //code to do if not exist.
  }
//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
//创建prefs映射以存储所有首选项
Map prefs=新的HashMap();
//将其放入prefs映射以关闭浏览器通知
prefs.put(“profile.default\u content\u setting\u values.notifications”,2);
//创建chrome选项以设置此优先级
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,prefs);
//现在使用chrome选项初始化chrome驱动程序,该选项将关闭chrome浏览器上的浏览器通知
WebDriver=新的ChromeDriver(选项);
//现在做进一步的步骤

希望对您有所帮助:)

请按照以下步骤操作:

A)使用JAVA:

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')
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);
对于旧Chrome版本(50):

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')
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);

B)使用PYTHON:

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')
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);
C)使用C:

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')
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);
它对我有用。
这里有更多详细信息:

到目前为止,我遇到的唯一有效的解决方案是:

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)

尚未接受任何答案,以下代码适用于我

红宝石,rspec,水豚

Capybara.register_driver :selenium_chrome do |app|
  prefs = {"profile.managed_default_content_settings.notifications" => 2,}
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: { prefs: prefs })

  Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: caps)
end

Capybara.javascript_driver = :selenium_chrome

如果您使用Ruby和Capybara,请尝试以下代码

    Capybara.register_driver :chrome_no_image_popup_maximize do |app|
    # 2: disable, other than 2 means enable it
    preferences = {
        "profile.managed_default_content_settings.notifications" => 2,
        "profile.managed_default_content_settings.images" => 2,
        "profile.managed_default_content_settings.popups" => 2   
    }

    caps = Selenium::WebDriver::Remote::Capabilities.chrome( 
        'chromeOptions' => {
            'prefs' => preferences, 
        } 
    )

    args = [ "--start-maximized" ]

    Capybara::Selenium::Driver.new(app, {:browser => :chrome, :desired_capabilities => caps, :args => args})
end

Capybara.default_driver = :chrome_no_image_popup_maximize
Capybara.javascript_driver = :chrome_no_image_popup_maximize

Facebook身份验证窗口显示一个覆盖继续作为[用户名]按钮的覆盖图

这会使“继续”按钮不可单击。为了避免这个问题,您可以使用JavaScript(不推荐)通过编程方式隐藏这些层,并使用此代码(不要这样做)


这个解决方案真的是Matthijs(请参见上面的评论)

让它手动允许一个。现在我正在做同样的事情!但是没有办法让selenium做到这一点吗?这里也一样,但我使用的是Codeception(php)。有什么想法吗?如果你不一定需要“允许”,但只是想摆脱对话框,你也可以模拟escape键,这将关闭对话框<代码>新操作().sendKeys(Keys.ESCAPE).build().perform()
browser.keys(“Escape”);//关闭“通知”对话框。
Matthijs,可以了!这适用于chromedriver的旧版本。(Chrome 49兼容版…)这不适用于更高版本。不确定何时切断,但对于最新版本,您需要使用prefs HashMap选项。Saurabh Gaur的回答。问题是关于默认情况下如何允许,
配置文件。默认内容设置值。通知=2
默认情况下将不允许,允许它应该是
profile.default\u content\u setting\u values.notifications=1
如何为safari执行此操作?这非常简洁,您已经为selenium提供了最常用的3种语言。不起作用,请检查此实例中的
浏览器是什么?谢谢,这正是我所需要的。如果像@Roymunson这样的人想知道的话,这就是Chrome。它适合我,在2019年2月使用Python 3.7和最新的selenium。对于像我这样的noobish用户,如果您在登录后使用selenium获取一些结果(除非您需要登录/单击按钮,否则使用请求包),而不是测试网页,关键行是:
from selenium import webdriver….options=webdriver.ChromeOptions()options.add_参数(--disable notifications”)driver=webdriver.Chrome(“path/to/ChromeDriver.exe”,options=options)
或OP希望的事实上启用通知,大概
(“--enable notifications”)
是主要的替代方案。