简体   繁体   English

如何通过 Selenium 在 chrome 中阻止弹出窗口

[英]How to block the pop-up windows in chrome by Selenium

I am using the java and selenium to write the test script for automation.我正在使用 java 和 selenium 编写自动化测试脚本。 On here the target automated Org has some reminder pop-up windows so it makes the confusion of taking in the command of driver.getWindowHandles() command on my results.在这里,目标自动化 Org 有一些提醒弹出窗口,因此它使我的结果中的driver.getWindowHandles()命令的命令变得混乱。

I try the below code to try to Block the pop-up windows on the chrome我尝试使用以下代码尝试阻止 chrome 上的弹出窗口

    System.setProperty("webdriver.chrome.driver", "//chrome path in system//");
    ChromeOptions options  = new ChromeOptions();
    options.setExperimentalOption("excludeSwitches", "disable-popup-blocking");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);

But it's not working it's given the below error and prevent the chrome to launch:但它不起作用,它给出了以下错误并阻止 chrome 启动:

from unknown error: cannot parse excludeSwitches来自未知错误:无法解析 excludeSwitches
from unknown error: must be a list来自未知错误:必须是列表

So what I should do for close those pop-up windows.那么我应该怎么做才能关闭这些弹出窗口。 Any answer is appreciated.任何答案表示赞赏。 I am using the chrome version: '2.29.461591' and java.version: '1.8.0_92'我正在使用chrome 版本:'2.29.461591'java.version:'1.8.0_92'

Try sending the "disable-popup-blocking" argument by type-casting it as a list.尝试通过将其作为列表进行类型转换来发送"disable-popup-blocking"参数。 This works for me in python:这在 python 中对我有用:

chrome_options.add_experimental_option("excludeSwitches", ["disable-popup-blocking"])

You can try below code it is working fine for me:您可以尝试以下代码,它对我来说很好用:

    System.setProperty("webdriver.chrome.driver", "//chrome path in system//");
    ChromeOptions options  = new ChromeOptions();
    //options.addArguments("incognito");
    options.addArguments("--disable-popup-blocking");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM