robocorp-browser

Browser configuration

Before a Playwright browser is started, it can be configured within the executing code.

Example

from robocorp import browser # Always use a headless Firefox browser browser.configure( browser_engine="firefox", headless=True, )

Options

browser_engine

Browser engine which should be used.

Valid values:

  • chromium (default)
  • chrome
  • chrome-beta
  • msedge
  • msedge-beta
  • msedge-dev
  • firefox
  • webkit

headless

Run the browser in headless mode.

If not defined (or value is None), the browser will automatically detect if it is an environment without a valid display device.

slowmo

Run interactions in slow motion, represented in milliseconds.

screenshot

Automatically capture a screenshot after each task.

Valid values:

  • on
  • off
  • only-on-failure

install

Install browser before starting. If not defined, download is only attempted if the browser fails to launch.

isolated

Used to define where the browser should be downloaded. If True, it'll be installed inside the isolated environment. If False (default) it'll be installed in a global cache folder.

persistent_context_directory

If a persistent context should be used, this should be the directory in which the persistent context should be stored/loaded from (it can be used to store the state of the automation to allow for sessions and cookies to be reused in a new automation).

See: Persistent Context Directory Guide

Persistent Context

It's possible to use a persistent context for automating with robocorp-browser.

When a persistent context is used, all the information which is obtained during the browser interaction (such as cookies, local storage, etc) will be saved in a specified directory and when the browser is opened afterwards, the same information will be loaded from the given directory.

To use this feature, it's possible to use the browser.configure API and pass the persistent_context_directory (note that this must be called before any browser page or context is actually created).

Example

from robocorp import browser browser.configure( persistent_context_directory="./persistent_context", ) page = browser.page()

Caveats

  • Note: it's recommended that only a single run uses a given persistent context at a time as the persistent context could include even open pages, which can be potentially troublesome at times.

    Sometimes it may be better just to save/restore the cookies (which can be done using the code below):

    from robocorp import browser import json cookies = json.dumps(browser.context().cookies()) ... # Save to disk (or shared resource such as `Vault`) and later restore with: browser.context().add_cookies(json.loads(cookies))
  • When using the persistent context, the robocorp.windows.browser() API will not be available as the browser is always created with a context, so, only robocorp.windows.context() is applicable.