RPA / Robocorp
Browsing / Configuration
Headless mode
tasks.py
from robocorp.tasks import task
from robocorp import browser
@task
def run_browser_headless():
"""
Configure browser to run in headless mode (invisible).
This is useful for running automation without a visible browser window,
especially in server environments or CI/CD pipelines.
"""
# Configure a headless browser (no visible UI)
browser_config = {
"headless": True, # Set to False to see the browser window
}
# Use the configuration when opening a page
page = browser.goto("https://smartworkmaster.com/", **browser_config)
# Perform actions as usual
title = page.title()
print(f"Page title: {title}")
# Take a screenshot to verify the page loaded correctly in headless mode
page.screenshot(path="headless_screenshot.png")
Custom viewport size
tasks.py
from robocorp.tasks import task
from robocorp import browser
@task
def set_custom_viewport():
"""
Configure browser with a custom viewport size.
Useful for testing responsive designs or automating on specific screen sizes.
"""
# Create browser context with custom viewport size
context = browser.context(
viewport={"width": 1920, "height": 1080} # Full HD resolution
)
# Create a page using this context
page = context.new_page()
page.goto("https://smartworkmaster.com/")
# View information about the current viewport
viewport_size = page.viewport_size()
print(f"Current viewport size: {viewport_size}")
# You can also set a mobile viewport with device emulation
mobile_context = browser.context(
viewport={"width": 375, "height": 812}, # iPhone X size
user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1"
)
# Open a mobile page
mobile_page = mobile_context.new_page()
mobile_page.goto("https://smartworkmaster.com/")
# Take screenshots to compare different viewports
page.screenshot(path="desktop_view.png")
mobile_page.screenshot(path="mobile_view.png")
Timeout settings
tasks.py
from robocorp.tasks import task
from robocorp import browser
from robocorp.browser import PlaywrightTimeoutError
@task
def configure_timeouts():
"""
Configure various timeout settings for browser automation.
This helps with handling slow websites or network conditions.
"""
# Open a page with global timeout settings
browser_config = {
"timeout": 30000, # Default timeout in milliseconds (30 seconds)
}
page = browser.goto("https://smartworkmaster.com/", **browser_config)
try:
# Set a specific timeout for this action (wait for a slow element)
# This is useful for elements that take longer to appear
page.click("button.slow-loading-button", timeout=60000) # 60 seconds
except PlaywrightTimeoutError:
print("The element didn't appear within the timeout period")
# Set page navigation timeout (for slow page loads)
page.set_default_navigation_timeout(45000) # 45 seconds
# Set default timeout for all other operations
page.set_default_timeout(15000) # 15 seconds
# Example: Wait for a network request to complete with custom timeout
with page.expect_response("**/api/data", timeout=30000) as response_info:
page.click("button#load-data")
response = response_info.value
print(f"Response status: {response.status}")