Skip to main content

A browser for your agent, built on Playwright.

Project description

A browser for your agent, built on Playwright.

Lint and Test PyPI version

Installation

pip install agentbrowser

Usage

Importing into your project

from agentbrowser import (
    get_browser,
    init_browser,
    navigate_to,
    get_body_html,
    get_body_text,
    get_document_html,
    create_page,
    close_page,
    evaluate_javascript,
)

Quickstart

from agentbrowser import (
    navigate_to,
    get_body_text,
)

# Navigate to a URL
page = navigate_to("https://google.com")

# Get the text from the page
text = get_body_text(page)

print(text)

API Documentation

ensure_event_loop()

Ensure that there is an event loop in the current thread. If no event loop exists, a new one is created and set for the current thread. This function returns the current event loop.

Example usage:

loop = ensure_event_loop()

get_browser()

Get a Playwright browser. If the browser doesn't exist, initializes a new one.

Example usage:

browser = get_browser()

init_browser(headless=True, executable_path=None)

Initialize a new Playwright browser.

Parameters:

  • headless: Whether the browser should be run in headless mode, defaults to True.
  • executable_path: Path to a Chromium or Chrome executable to run instead of the bundled Chromium.

Example usage:

init_browser(headless=False, executable_path="/usr/bin/google-chrome")

create_page(site=None)

Create a new page in the browser. If a site is provided, navigate to that site.

Parameters:

  • site: URL to navigate to, defaults to None.

Example usage:

page = create_page("https://www.example.com")

close_page(page)

Close a page.

Parameters:

  • page: The page to close.

Example usage:

page = create_page("https://www.example.com")
close_page(page)

navigate_to(url, page, wait_until="domcontentloaded")

Navigate to a URL in a page.

Parameters:

  • url: The URL to navigate to.
  • page: The page to navigate in.

Example usage:

page = create_page()
navigate_to("https://www.example.com", page)

get_document_html(page)

Get the HTML content of a page.

Parameters:

  • page: The page to get the HTML from.

Example usage:

page = create_page("https://www.example.com")
html = get_document_html(page)
print(html)

get_page_title(page)

Get the title of a page.

Parameters:

  • page: The page to get the title from.

Example usage:

page = create_page("https://www.example.com")
title = get_page_title(page)
print(title)

get_body_text(page)

Get the text content of a page's body.

Parameters:

  • page: The page to get the text from.

Example usage:

page = create_page("https://www.example.com")
text = get_body_text(page)
print(text)

get_body_html(page)

Get the HTML content of a page's body.

Parameters:

  • page: The page to get the HTML from.

Example usage:

page = create_page("https://www.example.com")
body_html = get_body_html(page)
print(body_html)

screenshot_page(page)

Get a screenshot of a page.

Parameters:

  • page: The page to screenshot.

Example usage:

page = create_page("https://www.example.com")
screenshot = screenshot_page(page)
with open("screenshot.png", "wb") as f:
    f.write(screenshot)

evaluate_javascript(code, page)

Evaluate JavaScript code in a page.

Parameters:

  • code: The JavaScript code to evaluate.
  • page: The page to evaluate the code in.

Example usage:

page = create_page("https://www.example.com")
result = evaluate_javascript("document.title", page)
print(result)

find_chrome()

Find the Chrome executable. Returns the path to the Chrome executable, or None if it could not be found.

Example usage:

chrome_path = find_chrome()
print(chrome_path)

Contributions Welcome

If you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentbrowser-0.2.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

agentbrowser-0.2.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file agentbrowser-0.2.2.tar.gz.

File metadata

  • Download URL: agentbrowser-0.2.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for agentbrowser-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a66cbc7c8aa4fe6434c6b0eb3f2b3cbff9bebab6c7f6e03013e5f23c6eec0042
MD5 c62a4ae2a34bded97d75dd784bb8ec36
BLAKE2b-256 abc0e3e9b60fba78a7c786d3bc1cfdcca73221cfb31b462bc6d14e62d481a990

See more details on using hashes here.

File details

Details for the file agentbrowser-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for agentbrowser-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24c5a10b1226eac6a40b345bb4222ee5df6d240e45da539abb5d4e3e76f8887c
MD5 f2ed56dbafcaf9eeb0d75ef5ff0066db
BLAKE2b-256 4b4fc10a1af50f0f778ecaba44f415922582c9ba9ff3cffb048587785b25e76e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page