ConsoleMessage
Source URL: https://playwright.dev/docs/api/class-consolemessage
ConsoleMessage | Playwright
Section titled “ConsoleMessage | Playwright”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('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 const msgPromise = page.waitForEvent('console'); await page.evaluate(() => { console.log('hello', 42, { foo: 'bar' }); // Issue console.log inside the page }); const msg = await msgPromise;
// Deconstruct console log arguments await msg.args()[0].jsonValue(); // hello await msg.args()[1].jsonValue(); // 42Methods
Section titled “Methods”Added before v1.9 consoleMessage.args
List of arguments passed to a console function call. See also page.on(‘console’).
Usage
consoleMessage.args();Returns
location
Section titled “location”Added before v1.9 consoleMessage.location
Usage
consoleMessage.location();Returns
URL of the resource.
* `lineNumber` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")0-based line number in the resource.
* `columnNumber` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")0-based column number in the resource.
Added in: v1.34 consoleMessage.page
The page that produced this console message, if any.
Usage
consoleMessage.page();Returns
Added before v1.9 consoleMessage.text
The text of the console message.
Usage
consoleMessage.text();Returns
Added before v1.9 consoleMessage.type
Usage
consoleMessage.type();Returns
- “log” | “debug” | “info” | “error” | “warning” | “dir” | “dirxml” | “table” | “trace” | “clear” | “startGroup” | “startGroupCollapsed” | “endGroup” | “assert” | “profile” | “profileEnd” | “count” | “time” | “timeEnd”#
Added in: v1.57 consoleMessage.worker
The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-null consoleMessage.page().
Usage
consoleMessage.worker();Returns