Robocorp Browser

2.4.0 · active · verified Thu Apr 16

The `robocorp-browser` package is a Python library for browser automation, acting as a lightweight wrapper around the Playwright project. It provides quality-of-life improvements, such as automatic lifecycle management for Playwright objects, and is primarily designed to be used within the Robocorp automation framework, especially with `robocorp-tasks`. The current version is 2.4.0. It is actively maintained as part of the broader Robocorp ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the browser, configure its behavior (like headless mode and slow motion), navigate to a URL, and interact with elements using the underlying Playwright API. It also shows how to integrate with `robocorp-tasks` and `robocorp-vault` for defining tasks and managing secrets.

import os
from robocorp import browser
from robocorp.tasks import task
from robocorp import vault # For secret management example

@task
def automate_browser_example():
    """Start a browser, navigate, and interact with a page."""
    # Configure browser settings (optional)
    browser.configure(
        browser_engine="chromium",
        headless=True, # Set to False to see the browser UI
        slowmo=100, # Run interactions in slow motion (milliseconds)
        screenshot="only-on-failure", # Capture screenshot on error
    )

    # Get a secret (example: login credentials)
    # In a real scenario, configure 'default-account' in Robocorp Vault
    # For quickstart, we use environment variables as a fallback
    username = os.environ.get('TEST_USERNAME', 'user')
    password = os.environ.get('TEST_PASSWORD', 'pass')
    
    try:
        account = vault.get_secret("default-account")
        username = account["username"]
        password = account["password"]
    except Exception:
        print("Warning: Could not get secret 'default-account'. Using environment variables or defaults.")

    # Navigate to a page (this will automatically launch the browser if not already open)
    page = browser.goto("https://www.example.com/login") # Replace with a real login page

    # Interact with elements using Playwright API
    # Example: fill login form (replace with actual selectors)
    # page.fill("#username_field", username)
    # page.fill("#password_field", password)
    # page.click("#login_button")

    # Perform some action, e.g., take a screenshot
    browser.screenshot(page=page, path="output/example_screenshot.png")
    print(f"Navigated to {page.url} and took a screenshot.")

    # The browser instance is automatically closed when the task finishes.

view raw JSON →