简体   繁体   中英

How to capture screen of pop up window using Selenium WebDriver?

I am trying to capture screen of popup window without successes.

i am using this code for "regular" capture screen:

File scrFile = ((TakesScreenshot)alertDialog).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

when pop-up is appear i want to capture the pop-up screen, how i can do it?

public void  checkPopup() throws IOException 
  {
   Alert alertDialog = driver.switchTo().alert();
   File scrFile =    ((TakesScreenshot)alertDialog).getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
   String alertText = alertDialog.getText();
  }

It is currently not possible. Webdriver's screenshot routine works on the DOM only; but the alert exists outside the DOM, it is a separate window, and the screenshot routine has no way to include it.

In Selenium's issue tracker, the problem has been reported and marked as Working As Intended: https://code.google.com/p/selenium/issues/detail?id=4412

It is not possible to take screenshot with the alert box using selenium. Either you need to accept or decline the alert box. Without doing so, it is not possible to capture screenshot. The UnexpectedAlertPresentException is thrown when you do not deal with the alert box. In my scenario, I accept the alert box and take the screenshot of the URL.

Following is the code fragment.

                    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:\\Images";
                    File screenShot = new File(imageDetails).getAbsoluteFile();
                    FileUtils.copyFile(outputFile, screenShot);
                    System.out.println("Screenshot saved: {}" + imageDetails);
                    driver.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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