Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SeleniumWebDriver中使用Robot框架拍摄屏幕截图_Java_Selenium_Selenium Webdriver_Phantomjs - Fatal编程技术网

Java 如何在SeleniumWebDriver中使用Robot框架拍摄屏幕截图

Java 如何在SeleniumWebDriver中使用Robot框架拍摄屏幕截图,java,selenium,selenium-webdriver,phantomjs,Java,Selenium,Selenium Webdriver,Phantomjs,我正在使用selenium进行java自动化项目。当出现故障时,需要拍摄web视图的屏幕截图。使用了TakesScreenshot,它在chrome驱动程序和phantomjs驱动程序中都运行良好 但当出现警报框时,此操作将失败。经过一些研究,我了解到如果存在警报,Selenium无法截图。必须首先处理警报。我可以使用java.awt.Robot,在这种情况下,我的屏幕截图中需要警报框 但是,如果使用phantomjs驱动程序或chrome正在运行,Robot将截取我的屏幕,并且不会获得web视

我正在使用selenium进行java自动化项目。当出现故障时,需要拍摄web视图的屏幕截图。使用了
TakesScreenshot
,它在chrome驱动程序和phantomjs驱动程序中都运行良好

但当出现警报框时,此操作将失败。经过一些研究,我了解到如果存在警报,Selenium无法截图。必须首先处理警报。我可以使用
java.awt.Robot
,在这种情况下,我的屏幕截图中需要警报框

但是,如果使用phantomjs驱动程序或chrome正在运行,Robot将截取我的屏幕,并且不会获得web视图。但是我需要一个带有警告框的屏幕截图(表示故障情况)


这个问题还有其他解决方案吗?

如果您真的想用警报捕捉屏幕,那么有一种方法。将用于截图的代码部分放在try-catch块中。如果发现任何警报,它将抛出异常并在catch块中处理它

代码段:

Alert alert = null;

    try {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("screenshot.png"));
    } catch (Exception e) {
        alert = driver.switchTo().alert();
        if(e.getMessage().contains("unexpected alert open:")){
            //before taking screenshot, you may wait for some moment to be properly visible
            try {
                 BufferedImage screencapture = new Robot().createScreenCapture((new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())));

                     File file = new File("screenshot.jpg");
                     ImageIO.write(screencapture, "png", file);
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    alert.accept(); //or you can use dismiss();

注意:要使用robot拍摄屏幕截图,您的窗口必须可见。

是否有任何特定的依赖项使您在最小化窗口中运行它?因为它不是最小化的,机器人应该给你所需要的。包括“最小化”,因为,如果我在同一个系统中做一些其他工作,而自动化过程在最小化的浏览器中运行。无论如何,如果这将使用phantomjs,
Robot
不会正确显示所需的图像?Robot屏幕截图就像敲击键盘上的prtscrn按钮一样。。所以如果它捕捉到屏幕上所有可见的东西。。看看你是否对活动进行计时,以便在警报框出现时窗口最大化。好的。这意味着,如果使用
phantomjs
驱动程序,就无法获得屏幕截图?在这种情况下,请建议另一种解决方案。我找不到任何谷歌搜索。如果是网络自动化。。您可以使用SeleniumWebDriver自动化web部件,而屏幕截图部分可以在java的Robot类中轻松完成。这里的RobotFramework指的是Java中的Robot类或基于python的RobotFramework?“要使用Robot截图,您的窗口必须可见”。如果我使用的是
phantomjs
webdriver。机器人不会帮忙的。