org.openqa.selenium.UnhandledAlertException: unexp

2020-01-27 04:23发布

I am using a Chrome Driver and trying to test a webpage.

Normally it runs fine, but sometimes I get exceptions:

 org.openqa.selenium.UnhandledAlertException: unexpected alert open
 (Session info: chrome=38.0.2125.111)
 (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not  provide any stacktrace information)
 Command duration or timeout: 16 milliseconds: null
 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
 System info: host: 'Casper-PC', ip: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version:  '6.1', java.version: '1.8.0_25'
 Driver info: org.openqa.selenium.chrome.ChromeDriver

Then I tried to handle the alert:

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

But this time I received:

org.openqa.selenium.NoAlertPresentException 

I am attaching the screenshots of the alert: First Alert and by using esc or enter i gets the second alertSecond Alert

I am not able to figure out what to do now. The problem is that I do not always receive this exception. And when it occurs, the test fails.

10条回答
劳资没心,怎么记你
2楼-- · 2020-01-27 05:24

After click event add this below code to handle

    try{
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       }
 catch (org.openqa.selenium.UnhandledAlertException e) {                
         Alert alert = driver.switchTo().alert(); 
         String alertText = alert.getText().trim();
         System.out.println("Alert data: "+ alertText);
         alert.dismiss();}

... do other things driver.close();

查看更多
▲ chillily
3楼-- · 2020-01-27 05:25

I was facing the same issue and I made this below changes.

try {
    click(myButton);
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}

It worked amazingly.

查看更多
\"骚年 ilove
4楼-- · 2020-01-27 05:25

The below code will help to handle unexpected alerts in selenium

try{
} catch (Exception e) {
if(e.toString().contains("org.openqa.selenium.UnhandledAlertException"))
 {
    Alert alert = getDriver().switchTo().alert();
    alert.accept();
 }
}
查看更多
一纸荒年 Trace。
5楼-- · 2020-01-27 05:27
UnhandledAlertException 

is thrown when it encounters an unhanded alert box popping out. You need to set your code to act normally unless an alert box scenario is found. This overcomes your problem.

       try {
            System.out.println("Opening page: {}");
            driver.get({Add URL});
            System.out.println("Wait a bit for the page to render");
            TimeUnit.SECONDS.sleep(5);
            System.out.println("Taking Screenshot");
            File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            String imageDetails = "C:\\images";
            File screenShot = new File(imageDetails).getAbsoluteFile();
            FileUtils.copyFile(outputFile, screenShot);
            System.out.println("Screenshot saved: {}" + imageDetails);
        } catch (UnhandledAlertException ex) {
            try {
                Alert alert = driver.switchTo().alert();
                String alertText = alert.getText();
                System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
                alert.accept();
                File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                String imageDetails = "C:\\Users";
                File screenShot = new File(imageDetails).getAbsoluteFile();
                FileUtils.copyFile(outputFile, screenShot);
                System.out.println("Screenshot saved: {}" + imageDetails);
                driver.close();
            } catch (NoAlertPresentException e) {
                e.printStackTrace();
            }
        } 
查看更多
登录 后发表回答