WebDriver关闭弹出窗口 [英] WebDriver closing the popup

查看:457
本文介绍了WebDriver关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网页驱动程序测试因为弹出窗口出现而卡住了。如何在测试中关闭它?

I have web-driver test which is stuck because a pop-up window appears. How ca I close it in test?

这是我的代码:

@Test
 public void canGoToSomePage() throws Exception {

 final WebDriver webDriver = getFireFoxDriver();
 webDriver.get(getRouteAbsolute("Application.index"));


 WebElement someElement = webDriver.findElement(By.id("some_id_here"));
 someElement.click();

 // HERE I GOT AUTHENTICATION POP-UP I WANT TO CLOSE


 assertNotNull(webDriver.findElement(By.id("some_2_id")));

 }


推荐答案

试试这个,

 Alert alert = driver.switchTo().alert();
 alert.accept();

我之前从未使用过警报,我曾经使用JS沉默弹出。你也可以这样做,但我猜警报将是首选。

I have never used alert before, I used to silent the pop up using JS before. You could do that too, but i guess Alert would be the first choice.

编辑#1

以下是使用Java脚本静默弹出窗口的方法。请注意,必须在导致弹出窗口显示的单击之前执行它。根据您的弹出窗口是警告,确认还是提示,您必须使用以下内容。

Here is how to use Java script to silent the pop up. Note that it has to be executed BEFORE the click that causes the popup to show up. Based on whether your pop up is alert, confirm or prompt, you will have to use something like below.

((JavascriptExecutor)driver).executeScript("window.alert = function(msg) { return true; }");
((JavascriptExecutor)driver).executeScript("window.confirm = function(msg) { return true; }");
((JavascriptExecutor)driver).executeScript("window.prompt = function(msg) { return true; }");

这篇关于WebDriver关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆