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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33218794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:22:21  来源:igfitidea点击:

Enabling popup windows in Chrome by Selenium

javaseleniumselenium-webdriverselenium-chromedriver

提问by LoveLovelyJava

My apologies in advance if my question sounds primary, I am very new at QA and Selenium.

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

I am using Java and Selenium to write a test, at one of my test's step when I click on a button it is supposed to open another window but Chrome blocks the popup window, can I enable popup by Selenium?

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

回答by JRodDynamite

Well, you need to initialize the ChromeDriverwith a customized configuration which will disable the flag to block popups. From this site, the command line switch for it is disable-popup-blocking. So, using ChromeOptionsand DesiredCapabilities, you add the desired config using the DesiredCapabilities.setCapability()function.

好吧,您需要ChromeDriver使用自定义配置初始化 ,这将禁用该标志以阻止弹出窗口。从这个站点,它的命令行开关是disable-popup-blocking. 因此,使用ChromeOptionsand DesiredCapabilities,您可以使用该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);

EDIT:Just found the same solution on this site.

编辑:刚刚在本网站上找到了相同的解决方案。

回答by Eby

There is also another option to enable popup windows. Because sometimes your company may block you from accessing any application in admin mode. If the above method fails to work, you can use the below codes to enable pop ups.

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

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);
  • Use the above code before starting your test.
  • 在开始测试之前使用上面的代码。

回答by WoodenKitty

If anyone is still encountering this problem, it's probably because they are on an old version of ChromeDriver. Popup blocking was disabled by default from version 21+

如果有人仍然遇到这个问题,那可能是因为他们使用的是旧版本的 ChromeDriver。默认情况下,从 21+ 版本开始禁用弹出窗口阻止

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

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