# `Playwright.ConsoleMessage`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.3/lib/playwright/api/console_message.ex#L7)

[`ConsoleMessage`](`Playwright.ConsoleMessage`) objects are dispatched by page via the [`Page.on(:console)`](Playwright.Page.html#module-console) event.
For each console message logged in the page there will be corresponding event in the Playwright
context.

```elixir
# 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!()
# => 42
```

*Since Playwright 1.8.*

The examples alias the modules they use (`alias Playwright.{Locator, Page}`).

# `t`

```elixir
@type t() :: %Playwright.ConsoleMessage{
  _event: term(),
  _page: term(),
  _worker: term()
}
```

A `ConsoleMessage` handle. Compare handles with `==`, their fields are internal.

# `args`

```elixir
@spec args(t()) :: [Playwright.JSHandle.t()]
```

List of arguments passed to a `console` function call. See also [`Page.on(:console)`](Playwright.Page.html#module-console).

*Since Playwright 1.8.*

# `location`

```elixir
@spec location(t()) :: %{
  url: String.t(),
  line: integer(),
  column: integer(),
  lineNumber: integer(),
  columnNumber: integer()
}
```

*Since Playwright 1.8.*

# `page`

```elixir
@spec page(t()) :: nil | Playwright.Page.t()
```

The page that produced this console message, if any.

*Since Playwright 1.34.*

# `text`

```elixir
@spec text(t()) :: String.t()
```

The text of the console message.

*Since Playwright 1.8.*

# `timestamp`

```elixir
@spec timestamp(t()) :: number()
```

The timestamp of the console message in milliseconds since the Unix epoch.

*Since Playwright 1.59.*

# `type`

```elixir
@spec type(t()) :: String.t()
```

*Since Playwright 1.8.*

# `worker`

```elixir
@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.*

---

*Consult [api-reference.md](api-reference.md) for complete listing*
