# Capturing the entire webpage with WebExecute's “CapturePage”

It seems that "CapturePage" in WebExecute only captures the currently visible portion of a website:

Is there some way to capture the whole image of a WebSessionObject? Or to print it to PDF?

• In case anybody else is checking, WebImage just uses WebExecute["CapturePage"] under the hood. – Carl Lange May 7 '19 at 15:47
• Just a minor note: This is not possible in general, since many web pages have dynamic behavior, including behavior which loads more data when a web page is scrolled down to the 'end'. This sort of infinite scrolling prevents you from taking a 'full screenshot' of any page. – Arnoud Buzing Jul 23 '19 at 18:35
• You can automate scrolling – M.R. Aug 12 '19 at 2:18
• @M.R. How do you automate scrolling? I didn't see it in the documentation. – kickert Nov 13 '19 at 13:32
• @kickert I don’t know, this never really worked for me – M.R. Nov 13 '19 at 15:16

The WebExecute framework uses functions specified by the W3C WebDriver spec. Unfortunately, the "Screenshot" functionality doesn't specifically allow for taking a screenshot of the entire page - just the viewport.

However, you can do this, provided you set the browser to headless mode (that is, pass Visible->False when you start your web session), getting the document height and setting the window height to it.

session = StartWebSession["Chrome", Visible -> False];

WebExecute["OpenPage" -> "https://toughsoles.ie"]

WebExecute["SetWindowSize" -> {600, WebExecute["JavascriptExecute" -> "return document.body.scrollHeight"]}]

WebExecute["CapturePage"]

(* Output is full Image of the web page *)


The crucial parts here are:

1. Start your session with Visible->False
2. Get the optimum window height with WebExecute["JavascriptExecute" -> "return document.body.scrollHeight"]
3. Set the viewport height with SetWindowSize - you might want to vary the width depending on your intended website or use case.

There's a bit of nuance with getting the optimum viewport height that I have not dealt with - this answer is relevant.

• In my case I need Visibility -> True, is there a way to force a print to PDF? – M.R. May 8 '19 at 4:24
• No - at least, not in the WebDriver spec. However, you could call window.print() in javascript. You'd still have to navigate the print UI manually, though. That UI is uncontrollable and browser-specific. – Carl Lange May 8 '19 at 7:21
• That’s too bad because I needed to use this in combination with mathematica.stackexchange.com/q/197127/403 @CarlLange – M.R. May 8 '19 at 15:35
• If you had mentioned that in your question, I could have given you a better answer. This might be possible - You should be able to simply press the "Print to PDF" button in the print dialog using RobotTools after calling window.print(). – Carl Lange May 8 '19 at 17:11