2

I only need screenshots of the current viewport, particularly respecting the scroll position. Basically what the current Chrome driver does.

From what I've read it seems this should be entirely possible, but for some reason the screenshot commands always strive to take a full document screenshot. In fact the IE driver takes multiple viewport shots and then stitches them together.

I'm using the Ruby webdriver, and I can't see anyway to take a screenshot of just the viewport. Is it possible?

  • You might be able to crop the image. I'm not sure how you would get the scroll position, but you can use WebDriver to get the height of the current window and crop the image to that height. – Andrio Aug 05 '15 at 19:49

2 Answers2

5

There are two competing definitions of "screenshot" when dealing with WebDriver. The first is the definition used by the open-source project, where a screenshot is defined to be an image of the entire DOM of the loaded page. In this case, both the IE driver and the Firefox driver are doing the correct thing as defined by the project since 2010. The Chrome implementation of WebDriver is incorrect in returning an image of only the visible view port.

The W3C WebDriver specification, on the other hand, defines screenshots to be of the current view port only. This is at odds with the current open-source project's driver implementations (IE and Firefox), and can be expected to change in the future. Once the specification has attained "Candidate Recommendation" status, those drivers are likely to be updated.

As an aside, it should be noted that the IE driver in no way uses a "scroll-and-stitch" method to create its screenshots. It does resize the browser window large enough to display the entire DOM, as calculated at the moment the screenshot is requested, and take an image of that. However, please note that there are issues with IE 10 and 11 in producing these full-DOM screenshots depending on the bit-ness of your operating system and of the IEDriverServer executable you're using.

At present, you'll need to use a tool external to WebDriver to obtain view-port-only screenshots for IE and Firefox.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Thanks for the detailed response, I saw IE scroll to the top when taking a screenshot, so though it was stitching multiple shots together, but what you say about resizing the window makes more sense. I find it frustrating that there are competing definitions of "a screenshot". It seems obvious to me that there should be an option to take a shot of either the full canvas or just the viewport, as different use cases will require both. – Matt Bearman Aug 07 '15 at 08:13
  • Using Chrome, this "splains" why I'm pulling my hair out when I zoom in my page and the object I need to take the pic from slides out of view. Thanks .but your post is from 2015, we are in 2017, do you see any light at the end of the tunnel for the resolution of this ? – AlexD Sep 21 '17 at 16:08
1

This is a frequently changing issue..

AFAIK right now Chrome- and IEDriver both capture screenshots of the viewport only, while FFDriver captures a full-page screenshot (FF will change this in the future). See also my answer to this question.

Community
  • 1
  • 1
74nine
  • 838
  • 7
  • 13