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?
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:
Visible->False
WebExecute["JavascriptExecute" -> "return document.body.scrollHeight"]
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.
window.print()
in javascript. You'd still have to navigate the print UI manually, though. That UI is uncontrollable and browser-specific.
$\endgroup$
Commented
May 8, 2019 at 7:21
RobotTools
after calling window.print()
.
$\endgroup$
Commented
May 8, 2019 at 17:11
WebImage
just usesWebExecute["CapturePage"]
under the hood. $\endgroup$