PlaywrightEx (PlaywrightEx v0.2.0)

View Source

Elixir client for the Playwright node.js server.

Automate browsers like Chromium, Firefox, Safari and Edge. Helpful for web scraping and agentic AI.

Experimental

This is an early stage, experimental, version. The API is subject to change.

Getting started

  1. Add dependency

     # mix.exs
     {:playwright_ex, "~> 0.1"}
  2. Install playwright and browser

     npm --prefix assets i -D playwright
     npm --prefix assets exec -- playwright install chromium --with-deps
  3. Start connection (or add to supervision tree)

     {:ok, _} = PlaywrightEx.Supervisor.start_link(timeout: 1000)
  4. Use it

     alias PlaywrightEx.{Browser, BrowserContext, Frame}
    
     {:ok, browser} = PlaywrightEx.launch_browser(:chromium, timeout: 1000)
     {:ok, context} = Browser.new_context(browser.guid, timeout: 1000)
    
     {:ok, %{main_frame: frame}} = BrowserContext.new_page(context.guid, timeout: 1000)
     {:ok, _} = Frame.goto(frame.guid, "https://elixir-lang.org/", timeout: 1000)
     {:ok, _} = Frame.click(frame.guid, Selector.link("Install"), timeout: 1000)

References:

Comparison to playwright-elixir

playwright-elixir built on the python client and tried to provide a comprehensive client from the start. playwright_ex instead is a ground-up implementation. It is not intended to be comprehensive. Rather, it is intended to be simple and easy to extend.

Summary

Functions

Launches a new browser instance.

Send message to playwright.

Subscribe to playwright responses concerning a resource, identified by its guid, or its descendants. Messages in the format {:playwright_msg, %{} = msg} will be sent to pid.

Types

guid()

@type guid() :: String.t()

unknown_opt()

@type unknown_opt() :: {Keyword.key(), Keyword.value()}

Functions

launch_browser(type, opts)

@spec launch_browser(atom(), [[PlaywrightEx.BrowserType.launch_opt() | unknown_opt()]]) ::
  {:ok, %{guid: guid()}} | {:error, any()}

Launches a new browser instance.

Options

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

  • :channel (String.t/0) - Browser distribution channel.

  • :executable_path (String.t/0) - Path to a browser executable to run instead of the bundled one.

  • :headless (boolean/0) - Whether to run browser in headless mode.

  • :slow_mo - Slows down Playwright operations by the specified amount of milliseconds.

send(msg, timeout)

@spec send(%{guid: guid(), method: atom()}, timeout()) ::
  %{result: map()} | %{error: map()}

Send message to playwright.

Don't use this! Prefer Channels functions. If a function is missing, consider opening a PR to add it.

subscribe(pid \\ self(), guid)

@spec subscribe(pid(), guid()) :: :ok

Subscribe to playwright responses concerning a resource, identified by its guid, or its descendants. Messages in the format {:playwright_msg, %{} = msg} will be sent to pid.