通过 Selenium 在 Chrome 中启用弹出窗口 windows

Enabling popup windows in Chrome by Selenium

如果我的问题听起来很初级,我提前道歉,我是 QA 和 Selenium 的新手。

我正在使用 Java 和 Selenium 编写测试,在我的测试步骤之一中,当我单击一个按钮时,它应该打开另一个 window 但 Chrome 块弹出窗口 window,我可以通过 Selenium 启用弹出窗口吗?

好吧,您需要使用自定义配置初始化 ChromeDriver,这将禁用阻止弹出窗口的标志。来自这个 site, the command line switch for it is disable-popup-blocking. So, using ChromeOptions and DesiredCapabilities, you add the desired config using the DesiredCapabilities.setCapability() 函数。

ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("disable-popup-blocking");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);

编辑: 刚刚在 site.

上找到了相同的解决方案

还有另一个启用弹出窗口的选项windows。因为有时您的公司可能会阻止您以管理员模式访问任何应用程序。如果上述方法无效,您可以使用以下代码启用弹出窗口。

WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("chrome://settings/content");
Thread.sleep(4000);
driver.switchTo().frame("settings");
Thread.sleep(2000);
driver.findElement(By.xpath("//input[@type='radio' and @name='popups']")).click();
Thread.sleep(4000);
driver.findElement(By.id("content-settings-overlay-confirm"));
Thread.sleep(4000);
  • 在开始测试之前使用上面的代码。

如果有人仍然遇到这个问题,可能是因为他们使用的是旧版本的 ChromeDriver。从版本 21+

开始默认禁用弹出窗口阻止

参考:https://bugs.chromium.org/p/chromedriver/issues/detail?id=1291

我使用了这个解决方案:

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security");
ChromeDriver driver = new ChromeDriver(options);