Browsinator is a Python library for programmatically controlling and interacting with a web browser using the Chrome DevTools Protocol.
To install Browsinator, you'll need Python 3.6 or later. You can install it using pip:
pip install browsinator
Here's a basic example of how to use Browsinator:
from browsinator import Browser
Browser.start() # Start Chrome or ...
Browser.start_brave() # Start Brave
browser = Browser()
browser.load("https://example.com", wait=True)
result = browser.run_script("document.title")
print(f"Page title: {result}")
browser.match_network("api/data", lambda req, res, data: print(f"API data: {data}"))
browser.monitor_network()
For example calling Network.getResponseBody with a specific request ID:
result = browser.send_command("Network.getResponseBody", {"requestId": 1})
print(f"Response body: {result}")
browser.keyboard_type("Hello, World!")
browser.keyboard_press_enter()
browser.mouse_click_selector("#submit-button")
browser.close()
You can also customize the Chrome startup:
# Start Chrome with custom options
Browser.start(
path="/path/to/chrome", # Custom Chrome executable path
minimized=False, # Start Chrome in normal window (not minimized)
debug_port=9223 # Use a custom debugging port
)
browser = Browser(debug_port=9222)