如何在 selenium webdriver 中处理 Javascript 警报/弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27841988/
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-08-11 05:08:20  来源:igfitidea点击:

How to handle Javascript Alert/pop up window in selenium webdriver

javajavascriptseleniumselenium-webdriver

提问by Prabhakar Y

I am not sure whether selenium webdriver can handle Javascript alert/pop-up window.

我不确定 selenium webdriver 是否可以处理 Javascript 警报/弹出窗口。

I have a scenario like
1. User uploads a xls file and click on upload button
2. Alert/Pop-up window will be displayed . Click "OK" on window

我有一个场景,如
1. 用户上传一个 xls 文件并单击上传按钮
2. 将显示警报/弹出窗口。在窗口中单击“确定”

Am able to automate the above scenario but the Alert/pop-up window is displayed while running the scripts.

能够自动执行上述场景,但在运行脚本时会显示警报/弹出窗口。

Is their anyway workaround that we can handle javascript alert/pop-up window?

他们无论如何解决我们可以处理javascript警报/弹出窗口的方法吗?

采纳答案by Subh

You can also try waiting for the alert to appear and then accepting it.

您也可以尝试等待警报出现,然后接受它

Below is the code for that (after the upload button is clicked):

下面是代码(点击上传按钮后):

try{
   //Wait 10 seconds till alert is present
   WebDriverWait wait = new WebDriverWait(driver, 10);
   Alert alert = wait.until(ExpectedConditions.alertIsPresent());

   //Accepting alert.
   alert.accept();
   System.out.println("Accepted the alert successfully.");
}catch(Throwable e){
   System.err.println("Error came while waiting for the alert popup. "+e.getMessage());
}

回答by Margus

Mock it out. Call javascript behind the UI directly:

嘲笑它。直接在 UI 后面调用 javascript:

WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor)driver).executeScript("yourScript();");
}

回答by Zach

Switch to default content Dismiss alert after accepting "OK" Otherwise your alert is from a different window which you'll have to switch to in order to dismiss

切换到默认内容接受“确定”后关闭警报否则您的警报来自不同的窗口,您必须切换到该窗口才能关闭

driver.switchTo().alert().accept();    
driver.switchTo().alert().dismiss();  
driver.switchTo().alert().defaultConent();  

回答by Java By Kiran

There are the four methods that we would be using along with the Alert interface:

我们将与 Alert 接口一起使用四种方法:

void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.

void Dismiss() – 弹出窗口一出现,dismiss() 方法就会点击“取消”按钮。

void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.

void accept() – 弹出窗口出现后,accept() 方法会立即单击“确定”按钮。

String getText() – The getText() method returns the text displayed on the alert box.

String getText() – getText() 方法返回显示在警报框中的文本。

void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.

void sendKeys(String stringToSend) – sendKeys() 方法将指定的字符串模式输入到警报框中。

if(isAlertPresent(ldriver)){
Alert alert = ldriver.switchTo().alert(); 
System.out.println(alert.getText());
alert.accept();

}

}