【问题标题】:How to block the pop-up windows in chrome by Selenium如何通过 Selenium 阻止 chrome 中的弹出窗口
【发布时间】:2017-12-07 10:04:42
【问题描述】:

我正在使用 java 和 selenium 编写自动化测试脚本。在这里,目标自动化组织有一些提醒弹出窗口,因此在我的结果中接收 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 启动:

来自未知错误:无法解析 excludeSwitches
来自未知错误:必须是列表

那么我应该怎么做才能关闭这些弹出窗口。任何答案表示赞赏。我正在使用 chrome version: '2.29.461591'java.version: '1.8.0_92'

【问题讨论】:

  • 看看这个解决方法...stackoverflow.com/questions/7742852/…。我认为您应该查看“Md. Nazmul Haque Sarker”的答案,因为它似乎与当前的 chrome 版本更相关。
  • 感谢您的回复。我是测试自动化的新手。我不清楚那个链接。在您建议的答案上。 css_selector_for_iframe = 'iframe[name="settings"]' driver.get('chrome://settings/content') iframe = driver.find_element_by_css_selector(css_selector_for_iframe) driver.switch_to_frame(iframe) driver.find_element_by_name('popups').click () click_element(driver, '#content-settings-overlay-confirm') driver.switch_to_default_content()

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以试试下面的代码,它对我来说很好:

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

    【讨论】:

    • 对不起,伙计。它在隐身窗口中启动 chrome,但在运行我的自动化测试脚本时无法阻止弹出式浏览器窗口:(
    • 为您更新了代码,可能需要添加以在您的环境中设置 chrome 选项。
    • "需要添加以在您的环境中设置 chrome 选项。"意味着我已经手动阻止了弹出窗口..?因为它在我的测试中也未能阻止 chrome 的弹出式浏览器窗口:(
    猜你喜欢
    • 1970-01-01
    • 2010-10-14
    • 2010-11-05
    • 2013-05-08
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    相关资源
    最近更新 更多