【问题标题】:Capturing full screen shot of webpage content using selenium 3 webdriver使用 selenium 3 webdriver 捕获网页内容的全屏截图
【发布时间】:2016-09-22 08:22:55
【问题描述】:

我正在尝试拍摄网页的完整内容屏幕截图。但我只使用以下代码获取基于视图的屏幕截图。浏览器是 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();
            } 

我怎样才能做到这一点。

【问题讨论】:

  • 我们只能为网页的可见部分截屏,但我们没有任何选项可以通过向下/向上滚动来截取整个网页,甚至不能通过 PrintScreen/系统请求
  • 感谢您的回复,但如果使用 selenium ide for fire fox,我会设法截取整个内容的屏幕截图。但无法通过 java 代码实现

标签: java selenium firefox webdriver


【解决方案1】:

您可能想使用 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 3,浏览器是 firefox。您是否有任何特定于 Firefox 的代码 sn-p 会有所帮助。谢谢
  • 我已经尝试了上面的代码,它按照视图截屏,没有捕获我使用 selenium ide for firefox 能够实现的全部内容,所以当我试图实现相同的目标时,我面临的问题是网络驱动程序。
【解决方案2】:

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

试试这个实用程序:aShot

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

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 2020-01-29
    • 2023-01-23
    • 2022-01-13
    • 1970-01-01
    • 2017-02-10
    • 2014-07-15
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多