A browser for your agent, built on Playwright.
pip install agentbrowser
from agentbrowser import (
get_browser,
init_browser,
navigate_to,
get_body_html,
get_body_text,
get_document_html,
create_page,
close_page,
evaluate_javascript,
)
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)
Get a Playwright browser. If the browser doesn't exist, initializes a new one.
Example usage:
browser = get_browser()
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 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 a page.
Parameters:
page
: The page to close.
Example usage:
page = create_page("https://www.example.com")
close_page(page)
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 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 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 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 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)
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 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 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)
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.