Firefox alert box not detected with Selenium WebDriver

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

ERROR net.serenitybdd.core.Serenity - No alert is present (WARNING: The server did not provide any stacktrace information)

I get this error when I try to detect an alert with this code:

Alert alertBox = getDriver().switchTo().alert(); 

The alert popup does show up, but it is not exactly the same when I execute the operation via Selenium WebDriver and manualy. This might be the source of the problem but I don't know why the popup is different.

The alert when done manualy

The alert when done with Selenium

This is the function that calls the alert:

function confirm_remove() {     return window.confirm("Alert message"); } 

Thanks for your help

UPDATE:

Ok I found a way to bypass the problem but not really solve it.

I tried inserting waits and sleep like you suggested me to but it did not solve the problem, the alert was still undetected.

In my project I am using a Test class which calls steps from my Steps class which calls webElements from my Page class. The thing is that between the step which clicks on the "Delete" button and the step that manages the alert, Selenium loses the handling of the alert. So I regrouped those two steps and Selenium seems to handle the alert well.

回答1:

You might need to add code to wait for the alert to be visible. Selenium can't tell if JavaScript has finished executing.

waitForAlert(WebDriver driver) {    int i=0;    while(i++<5)    {         try         {             Alert alert = driver.switchTo().alert();             break;         }         catch(NoAlertPresentException e)         {           Thread.sleep(1000);           continue;         }    } } 


回答2:

A little more elegant solution :

WebDriverWait wait = new WebDriverWait(driver, timeout); wait.until(ExpectedConditions.alertIsPresent()); 

Use the WebDriverWait, every time you have dynamic element that are not present when the page is done loading, like alert, popupwindow, modal popup, hiden element which turn visible,.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!