skip to content

Screenshots and Logs

What a run hands you after a failure instead of a rerun: images of the page or a single element, browser and driver logs, and network events captured as they happen.

on this pageshow

explore

questions

16

In Selenium 4, what does Take Element Screenshot return when the element is taller than the viewport?

level: middleimportance: must knowfreq 61%

answer

  1. The window decides the size
  2. Scrolling picks the slice, not the size
  3. No stitching anywhere in the algorithm
  4. Paint region derived from the visual viewport
  5. Zero-size elements raise an error

basics

~20 s

Only the visible part. Take Element Screenshot scrolls the element into view and then paints from the viewport framebuffer, so an element taller than the window comes back clipped at the viewport edge, never stitched.

solid answer

~50 s

In Selenium 4 the element call maps to the W3C `Take Element Screenshot` command, and its remote end steps are fixed: check the browsing context is open, resolve the element reference, **scroll the element into view**, read the element's rectangle on the next animation frame, then draw a bounding box from the framebuffer. That last step clamps the painted region to the **visual viewport**: the paint width and height are the viewport's dimensions minus the rectangle's origin, so nothing outside the visible area is ever painted. A customs declaration form whose line-items table runs 3000 px tall therefore yields one viewport-high slice starting where the scroll put it, not the whole table. If the viewport has zero width or height, or the canvas ends up with no pixels, the command fails with `unable to capture screen`, which the Java client raises as `ScreenshotException`.

go deeper

for a junior

Recall that the image is limited to what is on screen. If the element is taller than the window, expect a clipped picture rather than the whole element, and do not go looking for a full-element option that does not exist.

for a middle

Explain the remote end steps in order, especially that it scrolls the element into view itself and then paints from the viewport framebuffer. State plainly that the viewport is the ceiling and that no stitching happens.

for a senior

Show judgment about what the crop is for: aim at a smaller element rather than fighting the viewport, and make the artefact say where it was scrolled so nobody reads a truncated table as a short one.

for a principal

Decide how far a suite should go to work around the viewport ceiling, whether by larger windows, narrower targets or accepting clipped crops, and be honest that each choice changes what the evidence actually proves.

## The command is a fixed sequence, not a hint `WebElement.getScreenshotAs` in Selenium 4 is a thin wrapper over one W3C WebDriver command, **Take Element Screenshot**, reached at `GET /session/{session id}/element/{element id}/screenshot`. The client sends the element id and nothing else. There is no scale factor, no region parameter and no full-element flag. Everything about the resulting image is decided by the remote end, which runs these steps: 1. If the current browsing context is no longer open, return `no such window`. 2. Handle any open user prompt according to the session's prompt handler. 3. Resolve the element id to a known element, failing with `stale element reference` if the node is no longer attached to the DOM. 4. **Scroll the element into view.** 5. When the user agent next runs its animation frame callbacks, read the element's rectangle. 6. **Draw a bounding box from the framebuffer** using that rectangle. 7. Encode the resulting canvas as a Base64 PNG and return it. Step 4 is why you do not have to scroll the element yourself first. Step 6 is why the image can still be smaller than the element. ## Where the clipping comes from The specification defines the paint region arithmetically rather than leaving it to the driver. The painted width is the **visual viewport's** width minus the smaller of the rectangle's x coordinate and `x + width`; the painted height is the viewport's height minus the smaller of `y` and `y + height`. Three consequences follow directly: - The **viewport is a hard ceiling**. The paint dimensions are derived from the viewport, never from the element, so no crop can be larger than the visible area. - **Scrolling only chooses the slice.** Step 4 brings the element's origin into view, and the paint then runs from there to the edge of the visible region. - There is **no stitching**. Nothing in the algorithm scrolls repeatedly and joins the pieces, so seams, sticky headers repeated per tile and lazy content loading mid-capture are simply not phenomena this command can produce. ## What that means for a long declaration form | Element on the customs declaration form | What the crop contains | |---|---| | The single-line **Declaration reference** field | The whole field, tightly cropped | | The **duty summary** panel, shorter than the window | The whole panel | | The **line-items table**, 3000 px tall in a 900 px window | Roughly one viewport-high slice from the scrolled origin down | | A field currently covered by a modal | The modal's pixels, because the paint comes from the framebuffer | | A collapsed control with zero width or height | An error, not an empty image | ## The errors it raises, and what each means - `stale element reference`, HTTP 404: the node is no longer attached to the DOM, raised by the Java client as `StaleElementReferenceException`. - `no such element`, HTTP 404: the id is not a known element for this session, raised as `NoSuchElementException`. - `no such window`, HTTP 404: the browsing context has gone, raised as `NoSuchWindowException`. - `unable to capture screen`, HTTP 500: the visual viewport has zero width or height, or the resulting canvas has no pixels, raised as `org.openqa.selenium.remote.ScreenshotException`. The zero-pixel case deserves special memory. A collapsed row on the declaration form does not hand you a blank PNG you can quietly attach to a report; it throws in the middle of your evidence path, which is a very different thing to debug at 2am. ## When the crop is not enough - **Aim at a smaller element.** The most reliable fix is to crop to what a reader actually needs, the invalid commodity-code row rather than the whole line-items table. - **Give the browser a bigger window.** The ceiling follows the viewport, so a larger window raises the maximum crop size; how a session's window is sized is its own subject. - **Fall back to the viewport image.** When the entire tall region matters, one page-level capture plus a note of where the page was scrolled is more honest than a crop that silently ends mid-row. - **Do not fake it by mutating the page.** Injecting script to collapse, resize or restyle the element changes the very thing the evidence is supposed to preserve. ## The mental model to carry into the interview Take Element Screenshot is **a camera aimed at the viewport with a mask**, not a scanner that traverses the document. The remote end aims the camera by scrolling, the mask is the element's bounding rectangle, and the film is only ever as large as the window. Every surprising outcome, from a truncated table to an unexpected modal in the crop to an exception on a hidden control, falls out of that single sentence.

  • Does the remote end scroll the element into view, or must the client do it first?
    The remote end does it: scrolling the element into view is a step of the Take Element Screenshot algorithm, run before the rectangle is read and the framebuffer painted. Scrolling yourself beforehand is redundant, and it does not change the size of the image, only which slice of a tall element ends up inside the viewport.
  • What error comes back if the element has zero width or height?
    `unable to capture screen`, HTTP 500, raised by the Java client as `ScreenshotException`. A canvas produced from a rectangle with a zero dimension has no pixels, and the specification makes that an error rather than an empty image, so a collapsed control throws inside your capture path instead of quietly attaching a blank file.
  • Why can a clipped element crop be worse evidence than a page-level image?
    Because a crop that ends mid-row still looks complete. A reader cannot tell from the PNG that the table continued below the fold, so truncation can suggest the missing rows were absent rather than off-screen. When the whole region matters, capture the viewport and record where the page was scrolled.

It is a photograph taken through a fixed window frame rather than a panoramic scan: the remote end can aim the camera at the top of a very long form, but it can only ever record what the frame shows.

saying these in an interview costs you the question

  • Says the command stitches several scrolled captures into one tall image
  • Believes the element's full height is captured regardless of window size
  • Thinks the client must scroll the element into view before capturing
  • Assumes a hidden or zero-size element yields a blank PNG
  • Claims the crop excludes overlays because an element was the receiver
open as a page

A Selenium suite reads browser console entries on Chrome but the same code throws on Firefox. Why, and how do you handle it?

level: seniorimportance: must knowfreq 62%

basics

~10 s

Log retrieval was never standardised in W3C WebDriver, so Selenium routes it to its own extension endpoint. ChromeDriver implements that endpoint and geckodriver does not, so the call throws UnsupportedCommandException on Firefox.

open as a page

In Selenium, why does a failure hook's element screenshot throw StaleElementReferenceException, and how do you make the capture safe?

level: seniorimportance: must knowfreq 43%

basics

~20 s

Element references die with the node they point at. Take Element Screenshot resolves the reference before it paints, so a hook capturing after the page re-rendered gets a stale element reference instead of an image.

open as a page

In Selenium 4, how do you capture a whole-page image in Firefox, and what changes when the session is remote?

level: seniorimportance: must knowfreq 52%

basics

~20 s

Firefox exposes getFullPageScreenshotAs through the HasFullPageScreenshot interface, backed by a Mozilla extension endpoint rather than a standard command. Over a remote session the object is a RemoteWebDriver, so you must wrap it with Augmenter before the cast succeeds.

open as a page

In Selenium's Java client, how do you read a page's browser console output, and what comes back?

level: juniorimportance: should knowfreq 58%

basics

~10 s

Call driver.manage().logs().get(LogType.BROWSER). It returns a LogEntries collection of LogEntry objects, each carrying a level, a timestamp in epoch milliseconds, and the message text. Each call drains the buffer it read.

open as a page

In Selenium 4, what does WebElement.getScreenshotAs capture compared with calling it on the driver?

level: juniorimportance: should knowfreq 52%

basics

~10 s

Calling getScreenshotAs on a WebElement returns a PNG cropped to that element's bounding rectangle. The driver-level call captures the page's whole visible area instead, so the receiver you choose decides the region.

open as a page

In Selenium 4, why must a BiDi network or console listener be registered before the step you are diagnosing?

level: juniorimportance: should knowfreq 41%

basics

~10 s

Selenium's BiDi events are push-only. Subscribing sends a session.subscribe command and the browser starts delivering from that moment; nothing earlier is replayed. A listener added after the failing click captures nothing about it.

open as a page

In Selenium 4, what is the difference between DriverService.Builder's withLogFile and withLogOutput?

level: middleimportance: should knowfreq 40%

basics

~20 s

Both name where the driver process's log goes. withLogFile(File) writes it to that file on disk; withLogOutput(OutputStream) sends the same output to any stream, such as System.err or an in-memory buffer. Neither changes how much is logged.

open as a page

Your Selenium suite records failed requests with getDevTools(); what breaks when the same suite runs on Firefox?

level: middleimportance: should knowfreq 53%

basics

~10 s

getDevTools() comes from HasDevTools, which in Selenium 4 only ChromiumDriver implements. On Firefox the cast throws ClassCastException, so the Firefox runs either die or record nothing, leaving that browser's failures with no request evidence.

open as a page

In Selenium 4, why does driver.getScreenshotAs() capture only the visible viewport rather than the whole page?

level: middleimportance: should knowfreq 72%

basics

~20 s

Because the W3C Take Screenshot command dumps the visual viewport's framebuffer. Its algorithm clamps the painted rectangle to the viewport's width and height, so however tall the document is, the returned PNG is one screen.

open as a page

A Selenium test on a translation-memory editor fails with a bare unknown error - how does ChromeDriver's own log find the real cause?

level: seniorimportance: should knowfreq 44%

basics

~20 s

ChromeDriver's own log records every command the client sent, its full JSON payload, and the browser's real reply. Start the driver with --log-level=DEBUG and --log-path, then read the last command before the failure and its response.

open as a page

What artefacts appear when you stitch viewport screenshots into one long page image, and how do you reduce them?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Fixed toolbars repeat in every slice, the last slice duplicates a band because scrolling clamps at the maximum offset, and lazy content differs between slices. Hide fixed chrome, pre-scroll, freeze motion, and paste at the real offset.

open as a page

In Selenium, how do you choose ChromeDriver's log level and log destination for a large parallel suite?

level: principalimportance: should knowfreq 33%

basics

~20 s

Trade detail against volume. Selenium 4 driver logs at DEBUG or above record every command payload, which is large and contains whatever the tests typed. Give each worker its own log path, append rather than truncate, and use readable timestamps.

open as a page

In Selenium, what do OutputType.FILE, OutputType.BYTES and OutputType.BASE64 return from getScreenshotAs?

level: juniorimportance: nice to knowfreq 38%

basics

~10 s

All three wrap the same base64 PNG the browser returned. FILE gives a temporary file the JVM deletes on exit, BYTES gives the raw PNG byte array, and BASE64 gives the encoded string unchanged.

open as a page

In Selenium 4, what does LoggingPreferences change, and under which capability key is it sent to Chrome?

level: middleimportance: nice to knowfreq 38%

basics

~10 s

LoggingPreferences maps each log type to a minimum severity the remote end should buffer, and it is set at session creation. For Chrome it travels under the vendor-prefixed capability key goog:loggingPrefs.

open as a page

In Selenium 4, what does subscribing to every BiDi network event in every test cost a suite?

level: seniorimportance: nice to knowfreq 31%

basics

~20 s

In Selenium 4, every subscribed BiDi event crosses the socket and is deserialised by the client, and the callback runs on a connection thread, not the test thread. Narrow the events, keep only failures, and unsubscribe at teardown.

open as a page