简体   繁体   English

如何使用 Selenium WebDriver 捕获弹出窗口的屏幕?

[英]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; Webdriver 的截图例程仅适用于 DOM; but the alert exists outside the DOM, it is a separate window, and the screenshot routine has no way to include it.但是alert存在于DOM之外,它是一个单独的窗口,截图例程没有办法包含它。

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在 Selenium 的问题跟踪器中,该问题已被报告并标记为按预期工作: https : //code.google.com/p/selenium/issues/detail? id =4412

It is not possible to take screenshot with the alert box using selenium.使用 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.当您不处理警报框时,会抛出UnexpectedAlertPresentException In my scenario, I accept the alert box and take the screenshot of the URL.在我的场景中,我接受警告框并截取 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();

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

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