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

1 Answer 1

15
$\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.

$\endgroup$
4
  • $\begingroup$ In my case I need Visibility -> True, is there a way to force a print to PDF? $\endgroup$
    – M.R.
    Commented May 8, 2019 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
    Commented May 8, 2019 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.
    Commented May 8, 2019 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
    Commented May 8, 2019 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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