Skip to content

API Reference parrot_tools.scraping.drivers.playwright_driver

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.scraping.drivers.playwright_driver

View source

Playwright-based browser automation driver.

Implements :class:AbstractDriver using Playwright's async API, providing full browser automation with Playwright-exclusive features such as request interception, HAR recording, tracing, PDF export, and session persistence.

Alias AbstractDriver

Re-exported from parrot_tools.scraping.drivers.abstract.AbstractDriver.

Alias PlaywrightConfig

Re-exported from parrot_tools.scraping.drivers.playwright_config.PlaywrightConfig.

Class PlaywrightDriver

Playwright-based browser automation driver.

Implements the full AbstractDriver interface using Playwright's async API, plus Playwright-exclusive capabilities (HAR, tracing, PDF, interception, session persistence).

The playwright package is imported lazily inside :meth:start so the module can be loaded even when Playwright is not installed.

Parameter Type Default Description
config Optional[PlaywrightConfig] None Browser and context configuration. Defaults to PlaywrightConfig() (headless Chromium).

Attributes:

Attribute Type Description
recording_path Optional[str]
current_url str The URL of the current page.

Method PlaywrightDriver.start

Launch browser and create a default context + page.

When config.engine == "obscura", no browser is launched. Instead, Playwright connects to a supervised Obscura CDP endpoint via chromium.connect_over_cdp() (Obscura speaks CDP) and reuses its default context/page if one already exists, falling back to creating a new context/page otherwise. Process ownership of the Obscura endpoint stays with parrot.mcp.obscura.ObscuraProcessManager — this driver never starts or stops that process.

Otherwise (config.engine == "playwright", the default), behavior is unchanged: when config.user_data_dir is set, a persistent context is launched over that profile directory instead of a fresh browser + incognito context, so the page sees the profile's cookies, saved sessions and credential store (config.channel="chrome" opens a real installed Google Chrome for real Chrome profiles; config.executable_path points at a binary outside the channel's hardcoded install location).

start() -> None

Method PlaywrightDriver.quit

Close browser and release resources.

Only Playwright-owned resources are closed here. self._context is closed only when this driver created it (self._owns_context) — a reused Obscura default context (config.engine == "obscura" connecting to an endpoint that already had one) is left open, since closing it would tear down state a supervised process's other clients may still depend on. In config.engine == "obscura" (CDP) mode, Browser.close() disconnects Playwright's client — per Playwright's own CDP semantics it does not terminate the remote browser process, so the externally supervised Obscura process is left running for parrot.mcp.obscura.ObscuraProcessManager to manage.

quit() -> None

Method PlaywrightDriver.navigate

Navigate to url.

navigate(url: str, timeout: int = 30) -> None

Parameter Type Default Description
url str
timeout int 30

Method PlaywrightDriver.go_back

Navigate back.

go_back() -> None

Method PlaywrightDriver.go_forward

Navigate forward.

go_forward() -> None

Method PlaywrightDriver.reload

Reload the current page.

reload() -> None

Method PlaywrightDriver.click

Click element matching selector.

click(selector: str, timeout: int = 10) -> None

Parameter Type Default Description
selector str
timeout int 10

Method PlaywrightDriver.fill

Fill input matching selector with value.

fill(selector: str, value: str, timeout: int = 10) -> None

Parameter Type Default Description
selector str
value str
timeout int 10

Method PlaywrightDriver.select_option

Select an option in a <select> element.

select_option(selector: str, value: str, *, by: str = 'value', timeout: int = 10) -> None

Parameter Type Default Description
selector str CSS selector for the select element.
value str Value, visible text, or index string to select.
by str 'value' Selection mode — "value", "text" (Playwright label), or "index".
timeout int 10 Maximum wait time in seconds.

Method PlaywrightDriver.hover

Hover over element matching selector.

hover(selector: str, timeout: int = 10) -> None

Parameter Type Default Description
selector str
timeout int 10

Method PlaywrightDriver.press_key

Press a keyboard key.

press_key(key: str) -> None

Parameter Type Default Description
key str

Method PlaywrightDriver.get_page_source

Return the full HTML of the current page.

In Obscura mode, falls back to a main-world evaluate read when page.content() fails — Obscura breaks Playwright's utility world after a renderer-initiated navigation (e.g. a form submit).

get_page_source() -> str

Returns: str

Method PlaywrightDriver.get_text

Return the inner text of the first matching element.

In Obscura mode, falls back to a main-world evaluate read when the locator fails with a non-timeout error (see :meth:get_page_source).

get_text(selector: str, timeout: int = 10) -> str

Parameter Type Default Description
selector str
timeout int 10

Returns: str

Method PlaywrightDriver.get_attribute

Return the value of attribute on the matching element.

get_attribute(selector: str, attribute: str, timeout: int = 10) -> Optional[str]

Parameter Type Default Description
selector str
attribute str
timeout int 10

Returns: Optional[str]

Method PlaywrightDriver.get_all_texts

Return inner text of every matching element.

get_all_texts(selector: str, timeout: int = 10) -> List[str]

Parameter Type Default Description
selector str
timeout int 10

Returns: List[str]

Method PlaywrightDriver.screenshot

Take a screenshot and save to path.

In Obscura mode, falls back to a raw CDP Page.captureScreenshot when Playwright's screenshot fails with a non-timeout error (see :meth:get_page_source).

screenshot(path: str, full_page: bool = False) -> bytes

Parameter Type Default Description
path str
full_page bool False

Returns: bytes

Method PlaywrightDriver.type_text

Type keystrokes into a selected or currently focused element.

type_text(text: str, selector: Optional[str] = None, delay_ms: int = 0, clear_first: bool = False, timeout: int = 10) -> None

Parameter Type Default Description
text str
selector Optional[str] None
delay_ms int 0
clear_first bool False
timeout int 10

Method PlaywrightDriver.screenshot_element

Capture a PNG of a selected element and save it to path.

screenshot_element(selector: str, path: str, timeout: int = 10) -> bytes

Parameter Type Default Description
selector str
path str
timeout int 10

Returns: bytes

Method PlaywrightDriver.wait_for_selector

Wait for selector to reach state.

wait_for_selector(selector: str, timeout: int = 10, state: str = 'visible') -> None

Parameter Type Default Description
selector str
timeout int 10
state str 'visible'

Method PlaywrightDriver.wait_for_navigation

Wait for a navigation event to complete.

wait_for_navigation(timeout: int = 30) -> None

Parameter Type Default Description
timeout int 30

Method PlaywrightDriver.wait_for_load_state

Wait until the page reaches the given load state.

In Obscura mode, "load" and "domcontentloaded" are resolved by polling document.readyState: Obscura does not emit those lifecycle events after a renderer-initiated navigation, so Playwright's own wait would always time out.

wait_for_load_state(state: str = 'load', timeout: int = 30) -> None

Parameter Type Default Description
state str 'load'
timeout int 30

Method PlaywrightDriver.execute_script

Execute JavaScript with arguments in the page context.

execute_script(script: str, *args: Any) -> Any

Parameter Type Default Description
script str
*args Any ()

Returns: Any

Method PlaywrightDriver.evaluate

Evaluate a JavaScript expression and return the result.

evaluate(expression: str) -> Any

Parameter Type Default Description
expression str

Returns: Any

Method PlaywrightDriver.get_cookies

Return the browser context's cookies (HttpOnly included).

get_cookies(urls: Optional[Sequence[str]] = None) -> List[Dict[str, Any]]

Parameter Type Default Description
urls Optional[Sequence[str]] None Restrict to cookies that would be sent to these URLs; None = all.

Returns: List[Dict[str, Any]] — Cookie dicts as returned by Playwright's BrowserContext.cookies().

Raises:

  • RuntimeError — The driver has not been started yet (self._context is None).

Method PlaywrightDriver.intercept_requests

Register a request interception handler on all routes.

intercept_requests(handler: Callable) -> None

Parameter Type Default Description
handler Callable Async callable receiving (route, request).

Method PlaywrightDriver.intercept_by_resource_type

Block or modify requests by resource type.

intercept_by_resource_type(resource_types: List[str], action: str = 'abort') -> None

Parameter Type Default Description
resource_types List[str] Resource types to intercept (e.g. ["image", "stylesheet", "font"]).
action str 'abort' Action to take — "abort" (default) blocks the request.

Method PlaywrightDriver.mock_route

Mock network requests matching url_pattern.

mock_route(url_pattern: str, handler: Callable) -> None

Parameter Type Default Description
url_pattern str Glob pattern to match request URLs.
handler Callable Async callable to handle matched requests.

Method PlaywrightDriver.record_har

Log a note about HAR recording.

HAR recording in Playwright is configured at context creation time via :attr:PlaywrightConfig.record_har_path. This method exists for interface completeness.

record_har(path: str) -> None

Parameter Type Default Description
path str

Method PlaywrightDriver.save_pdf

Export the current page as a PDF (Chromium only).

save_pdf(path: str) -> bytes

Parameter Type Default Description
path str File path for the PDF output.

Returns: bytes — Raw PDF bytes.

Raises:

  • ValueError — If the browser type is not "chromium".

Method PlaywrightDriver.start_tracing

Start Playwright tracing.

Trace files can be viewed with: npx playwright show-trace trace.zip

start_tracing(name: str = 'trace', screenshots: bool = True, snapshots: bool = True) -> None

Parameter Type Default Description
name str 'trace'
screenshots bool True
snapshots bool True

Method PlaywrightDriver.stop_tracing

Stop tracing and save the trace archive to path.

stop_tracing(path: str) -> None

Parameter Type Default Description
path str

Method PlaywrightDriver.save_storage_state

Persist cookies and localStorage to a JSON file.

The saved state can be reused by setting PlaywrightConfig(storage_state=path) on a subsequent run.

save_storage_state(path: str) -> None

Parameter Type Default Description
path str File path for the state JSON.

Method PlaywrightDriver.new_page

Open a new tab within the same browser context.

The new page becomes the active page.

new_page() -> Any

Returns: Any — The new Playwright Page object.

Method PlaywrightDriver.get_network_responses

Return captured network responses.

get_network_responses() -> List[Dict[str, Any]]

Returns: List[Dict[str, Any]] — List of dicts with url, status, and body keys.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally