org.openqa.selenium.UnhandledAlertException: unexpected alert open

后端 未结 10 714
情歌与酒
情歌与酒 2020-11-30 06:33

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

Normally it runs fine, but sometimes I get exceptions:

 org.openqa.selenium.UnhandledAlertE         


        
相关标签:
10条回答
  • 2020-11-30 07:22

    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.

    0 讨论(0)
  • 2020-11-30 07:22

    Following is working for me

        private void acceptSecurityAlert() {
    
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS)          
                                                                .pollingEvery(3, TimeUnit.SECONDS)          
                                                                .ignoring(NoSuchElementException.class);    
        Alert alert = wait.until(new Function<WebDriver, Alert>() {       
    
            public Alert apply(WebDriver driver) {
    
                try {
    
                    return driver.switchTo().alert();
    
                } catch(NoAlertPresentException e) {
    
                    return null;
                }
            }  
        });
    
        alert.accept();
    }
    
    0 讨论(0)
  • 2020-11-30 07:25
    DesiredCapabilities firefox = DesiredCapabilities.firefox();
    firefox.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    

    You can use UnexpectedAlertBehaviour.ACCEPT or UnexpectedAlertBehaviour.DISMISS

    0 讨论(0)
  • 2020-11-30 07:26

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

    0 讨论(0)
  • 2020-11-30 07:26
    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();
                }
            } 
    
    0 讨论(0)
  • 2020-11-30 07:28

    Is your switch to alert within a try/catch block? You may also want to add a wait timeout to see if the alert shows up after a certain delay

    try {
        // Add a wait timeout before this statement to make 
        // sure you are not checking for the alert too soon.
        Alert alt = driver.switchTo().alert();
        alt.accept();
    } catch(NoAlertPresentException noe) {
        // No alert found on page, proceed with test.
    }
    
    0 讨论(0)
提交回复
热议问题