Skip to content

browser.screenshot#

Functions#

complete_page(file_name=None, destination_dir=None, delay_seconds=0.25) #

Take screenshot of complete page and save as PNG image.

Note

Firefox is recommended browser for complete page screenshots as it executes this in one go. Other browsers can't capture the entire page at once, and so we need to merge screenshots portion by portions – and this is obviously much slower. For example:

1
2
3
4
5
6
7
from browserist import Browser, BrowserSettings, BrowserType

settings = BrowserSettings(browser_type=BrowserType.FIREFOX)

with Browser(settings) as browser:
    browser.open.url("https://example.com")
    browser.screenshot.complete_page()

Parameters:

Name Type Description Default
file_name str | None

Name of the file. If None, the file name is generated automatically.

None
destination_dir str | Path | None

Destination directory. If None, the directory defined in the browser settings is used.

None
delay_seconds float

As we stitch several screenshots together by scrolling down the page, adjust iteration delay to ensure that the screen is updated after each scroll.

0.25
Example

Default file name and destination:

browser.screenshot.complete_page()

Custom file name and default destination:

browser.screenshot.complete_page("image.png")

Custom file name and destination:

browser.screenshot.complete_page("image.png", "/screenshots/folder")

Default file name and custom destination:

browser.screenshot.complete_page(destination_dir="/screenshots/folder")
Note

When setting a custom file name, screenshots should always be saved as a PNG file with a .png extension.

element(xpath, file_name=None, destination_dir=None) #

Take screenshot of visible portion and save as PNG image.

Parameters:

Name Type Description Default
xpath str

XPath of the element.

required
file_name str | None

Name of the file. If None, the file name is generated automatically.

None
destination_dir str | Path | None

Destination directory. If None, the directory defined in the browser settings is used.

None
Example

Default file name and destination:

browser.screenshot.element("//xpath/to/element")

Custom file name and default destination:

browser.screenshot.element("//xpath/to/element", "image.png")

Custom file name and destination:

browser.screenshot.element("//xpath/to/element", "image.png", "/screenshots/folder")

Default file name and custom destination:

browser.screenshot.element("//xpath/to/element", destination_dir="/screenshots/folder")
Note

When setting a custom file name, screenshots should always be saved as a PNG file with a .png extension.

visible_portion(file_name=None, destination_dir=None) #

Take screenshot of visible portion and save as PNG image.

Parameters:

Name Type Description Default
file_name str | None

Name of the file. If None, the file name is generated automatically.

None
destination_dir str | Path | None

Destination directory. If None, the directory defined in the browser settings is used.

None
Example

Default file name and destination:

browser.screenshot.visible_portion()

Custom file name and default destination:

browser.screenshot.visible_portion("image.png")

Custom file name and destination:

browser.screenshot.visible_portion("image.png", "/screenshots/folder")

Default file name and custom destination:

browser.screenshot.visible_portion(destination_dir="/screenshots/folder")
Note

When setting a custom file name, screenshots should always be saved as a PNG file with a .png extension.