使用selenium 3 webdriver捕获网页内容的全屏截图

时间:2016-09-22 08:22:55

标签: java selenium firefox webdriver

我正在尝试拍摄网页的完整内容屏幕截图。但我只使用以下代码获取基于视图的屏幕截图。浏览器是firefox我正在使用SELENIUM 3网络驱动程序。

File scrFile5 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            try {

                FileUtils.copyFile(scrFile5, new File("C:\\test.jpg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

我如何才能实现同样的目标。

2 个答案:

答案 0 :(得分:0)

您可能希望使用 RemoteWebDriver 界面。你在使用Firefox还是IE?见下文

File screenshotFile = null;
            try {
                // This checks to make sure we're not running an
                // incompatible driver to take a screenshot (e.g.
                // screenshots are not supported by the HtmlUnitDriver)

                //the following line will throw a ClassCastException if the current driver is not a browser typed driver
                RemoteWebDriver compatibleDriver = (RemoteWebDriver) driver;

                screenshotFile = ((TakesScreenshot) compatibleDriver).getScreenshotAs(OutputType.FILE);
            } catch (ClassCastException e) {
                //this driver does not support screenshots.
                // this should get logged as a warning -- this only means the driver cannot be of type RemoteWebDriver

            } finally {
                if (screenshotFile == null) {
                    System.out.println("This WebDriver does not support screenshots");
                    return;  //get us outa here
                }
            }

            try {

                String pathString = "some path of your choice"
                File path = new File(pathString);

                if (!path.exists())
                    path.mkdirs();

                stringPath.concat("/someFileName.png");

                File newFileLocation = new File(pathString);
                System.out.println("Full path to screenshot: " + newFileLocation.getAbsolutePath());

                FileUtils.copyFile(screenshotFile, newFileLocation);
            } catch (IOException e) {
                e.printStackTrace();
            }

答案 1 :(得分:0)

这是Selenium浏览器驱动程序的一个老问题。

试试这个实用程序:aShot

您可以在https://github.com/yandex-qatools/ashot

找到它