繁体   English   中英

使用Selenium 3 WebDriver捕获网页内容的全屏快照

[英]Capturing full screen shot of webpage content using selenium 3 webdriver

我正在尝试对网页进行完整的内容截屏。 但我只能通过以下代码获得基于视图的屏幕截图。 浏览器是Firefox,我正在使用SELENIUM 3 Web驱动程序。

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

我怎么能达到同样的目的。

您可能要使用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();
            }

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

试试这个工具:aShot

您可以在https://github.com/yandex-qatools/ashot中找到它

暂无
暂无

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

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