java - 如何通过Selenium阻止chrome中的弹出窗口

标签 java google-chrome selenium selenium-webdriver selenium-chromedriver

我正在使用java和selenium来编写自动化测试脚本。在这里,目标自动化 Org 有一些提醒弹出窗口,因此它使我在结果中接收 driver.getWindowHandles() 命令的命令变得困惑。

我尝试使用以下代码来尝试阻止 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);

但它不起作用,出现以下错误并阻止 chrome 启动:

from unknown error: cannot parse excludeSwitches
from unknown error: must be a list

那么我应该如何关闭这些弹出窗口呢?任何答案表示赞赏。我使用的是chrome版本:'2.29.461591'java.version:'1.8.0_92'

最佳答案

尝试通过将其类型转换为列表来发送“disable-popup-blocking”参数。 这在 python 中对我有用:

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

关于java - 如何通过Selenium阻止chrome中的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47692436/

相关文章:

google-chrome - 更新后在 Chrome 中无法登录 Blazor WASM

java - 从 url 获取 json 给出 NullPointerException

java - 使用与 Stream 内联的 Regex 或 StringTokenizer

php - 如果 Chrome 没有自动执行,如何在浏览器关闭时销毁或取消设置或类似 Chrome 中的 PHP session ?

java - 使用 selenium 定位框架集中的元素以及框架和 div 标签

python-3.x - 如何在 python 中使用 selenium 定位并单击 netflix 上的 "speed test"链接?

java - 在 Selenium WebDriver 中无法通过 java 代码运行 exe 文件

java - 为什么 thread.stop() 不起作用?

java - Hibernate 的 Activity 记录模式

html - 图片高度 : auto doesn't work on chrome