PlaywrightEx.Page (PlaywrightEx v0.3.2)

Copy Markdown View Source

Interact with a Playwright Page.

There is no official documentation, since this is considered Playwright internal.

References:

Summary

Functions

Dispatches a mousedown event at the current mouse position.

Moves the mouse to the specified coordinates.

Dispatches a mouseup event at the current mouse position.

Returns a screenshot of the page as binary data.

Updates the subscription for page events.

Types

mouse_down_opt()

@type mouse_down_opt() :: {:timeout, timeout()} | {:button, term()}

mouse_move_opt()

@type mouse_move_opt() ::
  {:timeout, timeout()} | {:x, integer() | float()} | {:y, integer() | float()}

mouse_up_opt()

@type mouse_up_opt() :: {:timeout, timeout()} | {:button, term()}

screenshot_opt()

@type screenshot_opt() ::
  {:timeout, timeout()}
  | {:full_page, boolean()}
  | {:omit_background, boolean()}

update_subscription_opt()

@type update_subscription_opt() ::
  {:timeout, timeout()} | {:event, atom()} | {:enabled, boolean()}

Functions

mouse_down(page_id, opts \\ [])

@spec mouse_down(PlaywrightEx.guid(), [mouse_down_opt() | PlaywrightEx.unknown_opt()]) ::
  {:ok, any()} | {:error, any()}

Dispatches a mousedown event at the current mouse position.

Reference: https://playwright.dev/docs/api/class-mouse#mouse-down

Example

# Perform a manual drag operation
{:ok, _} = Page.mouse_move(page_id, x: 100, y: 100, timeout: 5000)
{:ok, _} = Page.mouse_down(page_id, timeout: 5000)
{:ok, _} = Page.mouse_move(page_id, x: 200, y: 100, timeout: 5000)
{:ok, _} = Page.mouse_up(page_id, timeout: 5000)

Options

  • :timeout (timeout/0) - Required. Maximum time for the operation (milliseconds).

  • :button - Defaults to :left. The default value is :left.

mouse_move(page_id, opts \\ [])

@spec mouse_move(PlaywrightEx.guid(), [mouse_move_opt() | PlaywrightEx.unknown_opt()]) ::
  {:ok, any()} | {:error, any()}

Moves the mouse to the specified coordinates.

This method dispatches a mousemove event. Supports fractional coordinates for precise positioning.

Reference: https://playwright.dev/docs/api/class-mouse#mouse-move

Example

# Get element position
{:ok, result} = Frame.evaluate(frame_id,
  expression: "() => {
    const el = document.querySelector('.slider-handle');
    const box = el.getBoundingClientRect();
    return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
  }",
  is_function: true,
  timeout: 5000
)

# Move to element
{:ok, _} = Page.mouse_move(page_id, x: result["x"], y: result["y"], timeout: 5000)

Options

  • :timeout (timeout/0) - Required. Maximum time for the operation (milliseconds).

  • :x - Required. x coordinate relative to the main frame's viewport in CSS pixels.

  • :y - Required. y coordinate relative to the main frame's viewport in CSS pixels.

mouse_up(page_id, opts \\ [])

@spec mouse_up(PlaywrightEx.guid(), [mouse_up_opt() | PlaywrightEx.unknown_opt()]) ::
  {:ok, any()} | {:error, any()}

Dispatches a mouseup event at the current mouse position.

Reference: https://playwright.dev/docs/api/class-mouse#mouse-up

Example

# Right-click at current position
{:ok, _} = Page.mouse_down(page_id, button: :right, timeout: 5000)
{:ok, _} = Page.mouse_up(page_id, button: :right, timeout: 5000)

Options

  • :timeout (timeout/0) - Required. Maximum time for the operation (milliseconds).

  • :button - Defaults to :left. The default value is :left.

screenshot(page_id, opts \\ [])

@spec screenshot(PlaywrightEx.guid(), [screenshot_opt() | PlaywrightEx.unknown_opt()]) ::
  {:ok, binary()} | {:error, any()}

Returns a screenshot of the page as binary data.

Reference: https://playwright.dev/docs/api/class-page#page-screenshot

Options

  • :timeout (timeout/0) - Required. Maximum time for the operation (milliseconds).

  • :full_page (boolean/0) - When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Defaults to false.

  • :omit_background (boolean/0) - Hides default white background and allows capturing screenshots with transparency. Defaults to false. Not applicable to jpeg images.

update_subscription(page_id, opts \\ [])

@spec update_subscription(PlaywrightEx.guid(), [
  update_subscription_opt() | PlaywrightEx.unknown_opt()
]) ::
  {:ok, any()} | {:error, any()}

Updates the subscription for page events.

Reference: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/client/page.ts

Options

  • :timeout (timeout/0) - Required. Maximum time for the operation (milliseconds).

  • :event (atom/0) - Required.

  • :enabled (boolean/0) - The default value is true.