12
$\begingroup$

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

enter image description here

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

$\endgroup$
  • $\begingroup$ In case anybody else is checking, WebImage just uses WebExecute["CapturePage"] under the hood. $\endgroup$ – Carl Lange May 7 '19 at 15:47
  • $\begingroup$ 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. $\endgroup$ – Arnoud Buzing Jul 23 '19 at 18:35
  • $\begingroup$ You can automate scrolling $\endgroup$ – M.R. Aug 12 '19 at 2:18
  • $\begingroup$ @M.R. How do you automate scrolling? I didn't see it in the documentation. $\endgroup$ – kickert Nov 13 '19 at 13:32
  • 1
    $\begingroup$ @kickert I don’t know, this never really worked for me $\endgroup$ – M.R. Nov 13 '19 at 15:16
14
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ In my case I need Visibility -> True, is there a way to force a print to PDF? $\endgroup$ – M.R. May 8 '19 at 4:24
  • $\begingroup$ 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. $\endgroup$ – Carl Lange May 8 '19 at 7:21
  • $\begingroup$ That’s too bad because I needed to use this in combination with mathematica.stackexchange.com/q/197127/403 @CarlLange $\endgroup$ – M.R. May 8 '19 at 15:35
  • $\begingroup$ 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(). $\endgroup$ – Carl Lange May 8 '19 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.