简体   繁体   English

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

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

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 1.用户上传一个xls文件并点击上传按钮
2. Alert/Pop-up window will be displayed . 2. 将显示警报/弹出窗口。 Click "OK" on window在窗口中单击“确定”

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警报/弹出窗口的方法吗?

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

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

Mock it out.嘲笑它。 Call javascript behind the UI directly:直接在 UI 后面调用 javascript:

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

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

} }

Alert is a interface which have the abstract methods below Alert 是一个接口,具有以下抽象方法

void accept();
void dismiss();
String getText();
void sendKeys(String keysToSend);

new WebDriverWait(driver,10).
until(ExpectedConditions.alertIsPresent()).accept();

alertIsPresent() internally return the 
driver.switchTo.alert(); then we don't have to write it explicitly

hope this is been helpful

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM