ConsoleMessage objects are dispatched by page via the Page.on(:console) event.
For each console message logged in the page there will be corresponding event in the Playwright
context.
# Listen for all console logs
Page.on(page, :console, &IO.puts(ConsoleMessage.text(&1)))
# Listen for all console events and handle errors
Page.on(page, :console, fn msg ->
if ConsoleMessage.type(msg) == "error", do: IO.puts(~s(Error text: "#{ConsoleMessage.text(msg)}"))
end)
# Get the next console log
msg_promise = Page.wait_for_event(page, :console)
# Issue console.log inside the page
Page.evaluate!(page, "() => console.log('hello', 42, { foo: 'bar' })")
msg = Promise.await!(msg_promise)
# Deconstruct console log arguments
ConsoleMessage.args(msg) |> Enum.at(0) |> JSHandle.json_value!()
# => "hello"
ConsoleMessage.args(msg) |> Enum.at(1) |> JSHandle.json_value!()
# => 42Since Playwright 1.8.
The examples alias the modules they use (alias Playwright.{Locator, Page}).
Summary
Functions
List of arguments passed to a console function call. See also Page.on(:console).
Since Playwright 1.8.
The page that produced this console message, if any.
The text of the console message.
The timestamp of the console message in milliseconds since the Unix epoch.
Since Playwright 1.8.
The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-nil Playwright.ConsoleMessage.page/1.
Types
Functions
@spec args(t()) :: [Playwright.JSHandle.t()]
List of arguments passed to a console function call. See also Page.on(:console).
Since Playwright 1.8.
@spec location(t()) :: %{ url: String.t(), line: integer(), column: integer(), lineNumber: integer(), columnNumber: integer() }
Since Playwright 1.8.
@spec page(t()) :: nil | Playwright.Page.t()
The page that produced this console message, if any.
Since Playwright 1.34.
The text of the console message.
Since Playwright 1.8.
The timestamp of the console message in milliseconds since the Unix epoch.
Since Playwright 1.59.
Since Playwright 1.8.
@spec worker(t()) :: nil | Playwright.Worker.t()
The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-nil Playwright.ConsoleMessage.page/1.
Since Playwright 1.57.