PlaywrightEx (PlaywrightEx v0.2.0)
View SourceElixir 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
Add dependency
# mix.exs {:playwright_ex, "~> 0.1"}Install playwright and browser
npm --prefix assets i -D playwright npm --prefix assets exec -- playwright install chromium --with-depsStart connection (or add to supervision tree)
{:ok, _} = PlaywrightEx.Supervisor.start_link(timeout: 1000)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:
- Code extracted from phoenix_test_playwright.
- Inspired by playwright-elixir.
- Official playwright node.js client docs.
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
@type guid() :: String.t()
@type unknown_opt() :: {Keyword.key(), Keyword.value()}
Functions
@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 message to playwright.
Don't use this! Prefer Channels functions.
If a function is missing, consider opening a PR to add it.
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.