简体   繁体   中英

Selenium Webdriver take screenshot of viewport only

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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