2021-01-07 11:46:05 -08:00
|
|
|
# class: ConsoleMessage
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
[ConsoleMessage] objects are dispatched by page via the [`event: Page.console`] event.
|
2022-03-12 09:32:54 -08:00
|
|
|
For each console messages logged in the page there will be corresponding event in the Playwright
|
|
|
|
context.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Listen for all console logs
|
|
|
|
page.on('console', msg => console.log(msg.text()))
|
|
|
|
|
|
|
|
// Listen for all console events and handle errors
|
|
|
|
page.on('console', msg => {
|
|
|
|
if (msg.type() === 'error')
|
|
|
|
console.log(`Error text: "${msg.text()}"`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the next console log
|
2022-11-30 12:36:35 -08:00
|
|
|
const msgPromise = page.waitForEvent('console');
|
|
|
|
await page.evaluate(() => {
|
|
|
|
console.log('hello', 42, { foo: 'bar' }); // Issue console.log inside the page
|
|
|
|
});
|
|
|
|
const msg = await msgPromise;
|
2022-03-12 09:32:54 -08:00
|
|
|
|
|
|
|
// Deconstruct console log arguments
|
|
|
|
await msg.args[0].jsonValue() // hello
|
|
|
|
await msg.args[1].jsonValue() // 42
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
2023-01-17 11:04:43 -08:00
|
|
|
// Listen for all console messages and print them to the standard output.
|
2022-03-12 09:32:54 -08:00
|
|
|
page.onConsoleMessage(msg -> System.out.println(msg.text()));
|
|
|
|
|
2023-01-17 11:04:43 -08:00
|
|
|
// Listen for all console messages and print errors to the standard output.
|
2022-03-12 09:32:54 -08:00
|
|
|
page.onConsoleMessage(msg -> {
|
|
|
|
if ("error".equals(msg.type()))
|
|
|
|
System.out.println("Error text: " + msg.text());
|
|
|
|
});
|
|
|
|
|
2023-01-17 11:04:43 -08:00
|
|
|
// Get the next console message
|
2022-03-12 09:32:54 -08:00
|
|
|
ConsoleMessage msg = page.waitForConsoleMessage(() -> {
|
|
|
|
// Issue console.log inside the page
|
|
|
|
page.evaluate("console.log('hello', 42, { foo: 'bar' });");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Deconstruct console.log arguments
|
|
|
|
msg.args().get(0).jsonValue() // hello
|
|
|
|
msg.args().get(1).jsonValue() // 42
|
|
|
|
```
|
|
|
|
|
|
|
|
```python async
|
|
|
|
# Listen for all console logs
|
|
|
|
page.on("console", lambda msg: print(msg.text))
|
|
|
|
|
|
|
|
# Listen for all console events and handle errors
|
|
|
|
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
|
|
|
|
|
|
|
# Get the next console log
|
|
|
|
async with page.expect_console_message() as msg_info:
|
|
|
|
# Issue console.log inside the page
|
|
|
|
await page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
|
|
|
msg = await msg_info.value
|
|
|
|
|
|
|
|
# Deconstruct print arguments
|
|
|
|
await msg.args[0].json_value() # hello
|
|
|
|
await msg.args[1].json_value() # 42
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Listen for all console logs
|
|
|
|
page.on("console", lambda msg: print(msg.text))
|
|
|
|
|
|
|
|
# Listen for all console events and handle errors
|
|
|
|
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
|
|
|
|
|
|
|
# Get the next console log
|
|
|
|
with page.expect_console_message() as msg_info:
|
|
|
|
# Issue console.log inside the page
|
|
|
|
page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
|
|
|
msg = msg_info.value
|
|
|
|
|
|
|
|
# Deconstruct print arguments
|
|
|
|
msg.args[0].json_value() # hello
|
|
|
|
msg.args[1].json_value() # 42
|
|
|
|
```
|
|
|
|
|
|
|
|
```csharp
|
2023-01-30 09:10:22 -08:00
|
|
|
// Listen for all console messages and print them to the standard output.
|
2022-03-12 09:32:54 -08:00
|
|
|
page.Console += (_, msg) => Console.WriteLine(msg.Text);
|
|
|
|
|
2023-01-30 09:10:22 -08:00
|
|
|
// Listen for all console messages and print errors to the standard output.
|
2022-03-12 09:32:54 -08:00
|
|
|
page.Console += (_, msg) =>
|
|
|
|
{
|
|
|
|
if ("error".Equals(msg.Type))
|
|
|
|
Console.WriteLine("Error text: " + msg.Text);
|
|
|
|
};
|
|
|
|
|
2023-01-30 09:10:22 -08:00
|
|
|
// Get the next console message
|
2022-03-12 09:32:54 -08:00
|
|
|
var waitForMessageTask = page.WaitForConsoleMessageAsync();
|
|
|
|
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
|
|
|
|
var message = await waitForMessageTask;
|
|
|
|
// Deconstruct console.log arguments
|
|
|
|
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
|
|
|
|
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
|
|
|
|
```
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## method: ConsoleMessage.args
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- returns: <[Array]<[JSHandle]>>
|
|
|
|
|
2021-05-05 17:18:07 -03:00
|
|
|
List of arguments passed to a `console` function call. See also [`event: Page.console`].
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## method: ConsoleMessage.location
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-02-10 12:54:44 -08:00
|
|
|
* langs: js, python
|
2021-01-07 11:46:05 -08:00
|
|
|
- returns: <[Object]>
|
|
|
|
- `url` <[string]> URL of the resource.
|
|
|
|
- `lineNumber` <[int]> 0-based line number in the resource.
|
|
|
|
- `columnNumber` <[int]> 0-based column number in the resource.
|
|
|
|
|
2021-02-10 12:54:44 -08:00
|
|
|
## method: ConsoleMessage.location
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-02-10 12:54:44 -08:00
|
|
|
* langs: csharp, java
|
|
|
|
- returns: <[string]>
|
|
|
|
|
|
|
|
URL of the resource followed by 0-based line and column numbers in the resource formatted as `URL:line:column`.
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## method: ConsoleMessage.text
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- returns: <[string]>
|
|
|
|
|
2021-05-05 17:18:07 -03:00
|
|
|
The text of the console message.
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## method: ConsoleMessage.type
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- returns: <[string]>
|
|
|
|
|
|
|
|
One of the following values: `'log'`, `'debug'`, `'info'`, `'error'`, `'warning'`, `'dir'`, `'dirxml'`, `'table'`,
|
|
|
|
`'trace'`, `'clear'`, `'startGroup'`, `'startGroupCollapsed'`, `'endGroup'`, `'assert'`, `'profile'`, `'profileEnd'`,
|
|
|
|
`'count'`, `'timeEnd'`.
|