💡 Sync Version: This documentation covers the asynchronous API. For synchronous operations, see
Browser.⚡ Performance Advantage: Async API enables concurrent operations with 4-6x performance improvements for parallel tasks.
- Browser Use Guide - Complete guide to browser automation
The Browser module provides comprehensive browser automation capabilities including navigation, element interaction, screenshot capture, and content extraction. It enables automated testing and web scraping workflows.
- Requires
browser_latestimage for browser automation features
class AsyncBrowser(AsyncBaseService)Browser provides browser-related operations for the session.
def __init__(self, session: "AsyncSession")async def initialize(option: Optional["BrowserOption"] = None) -> boolInitialize the browser instance with the given options asynchronously. Returns True if successful, False otherwise.
Arguments:
optionBrowserOption, optional - Browser configuration options. If None, default options are used.
Returns:
bool: True if initialization was successful, False otherwise.
Example:
create_result = await agent_bay.create()
session = create_result.session
# Use default options
await session.browser.initialize()
# Or with specific options
browser_option = BrowserOption(use_stealth=True)
await session.browser.initialize(browser_option)
await session.delete()async def init(option: Optional["BrowserOption"] = None) -> boolAlias for initialize.
async def destroy()Destroy the browser instance manually.
async def screenshot(page, full_page: bool = False, **options) -> bytesTakes a screenshot of the specified page with enhanced options and error handling.
Arguments:
pagePage - The Playwright Page object to take a screenshot of. This is a required parameter.full_pagebool - Whether to capture the full scrollable page. Defaults to False. **options: Additional screenshot options that will override defaults. Common options include:- type (str): Image type, either 'png' or 'jpeg' (default: 'png')
- quality (int): Quality of the image, between 0-100 (jpeg only)
- timeout (int): Maximum time in milliseconds (default: 60000)
- animations (str): How to handle animations (default: 'disabled')
- caret (str): How to handle the caret (default: 'hide')
- scale (str): Scale setting (default: 'css')
Returns:
bytes: Screenshot data as bytes.
Raises:
BrowserError: If browser is not initialized.
RuntimeError: If screenshot capture fails.
async def get_endpoint_url() -> strReturns the endpoint URL if the browser is initialized, otherwise raises an exception. When initialized, always fetches the latest CDP url from session.get_link().
Returns:
str: The browser CDP endpoint URL.
Raises:
BrowserError: If browser is not initialized or endpoint URL cannot be retrieved.
Example:
create_result = await agent_bay.create()
session = create_result.session
browser_option = BrowserOption()
await session.browser.initialize(browser_option)
endpoint_url = await session.browser.get_endpoint_url()
print(f"CDP Endpoint: {endpoint_url}")
await session.delete()def get_option() -> Optional["BrowserOption"]Returns the current BrowserOption used to initialize the browser, or None if not set.
Returns:
Optional[BrowserOption]: The browser options if initialized, None otherwise.
Example:
create_result = await agent_bay.create()
session = create_result.session
browser_option = BrowserOption(use_stealth=True)
await session.browser.initialize(browser_option)
current_options = session.browser.get_option()
print(f"Stealth mode: {current_options.use_stealth}")
await session.delete()def is_initialized() -> boolReturns True if the browser was initialized, False otherwise.
Returns:
bool: True if browser is initialized, False otherwise.
Example:
create_result = await agent_bay.create()
session = create_result.session
print(f"Initialized: {session.browser.is_initialized()}")
browser_option = BrowserOption()
await session.browser.initialize(browser_option)
print(f"Initialized: {session.browser.is_initialized()}")
await session.delete()- Wait for page load completion before interacting with elements
- Use appropriate selectors (CSS, XPath) for reliable element identification
- Handle navigation timeouts and errors gracefully
- Take screenshots for debugging and verification
- Clean up browser resources after automation tasks
Related APIs:
Documentation generated automatically from source code using pydoc-markdown.