Read the console
Listen to page console messages by type and text.
This example registers a console handler, loads a page that emits log, warn, and error, then triggers another message with evaluate().
Use it to learn the order: register the handler first, then navigate or run the code that emits console messages.
Run it
php content/examples/console-messages.php
Expected output:
[LOG] Hello from the page console
[WARNING] Heads up! This is a warning message.
[ERROR] Something went wrong.
[INFO] Additional info message from evaluate()
Line numbers may appear depending on browser metadata.
What it demonstrates
- Console messages are runtime events from the page.
- The handler receives message type, text, and location.
evaluate()can trigger browser-side console output.- A short delay gives asynchronous console delivery time to flush in this standalone script.
Go next
- Learn runtime events: Console
- Capture logs: Logs
- Evaluate JavaScript: JavaScript
- API reference: ConsoleMessage
Source
console-messages.php<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../../vendor/autoload.php';
use Playwright\Console\ConsoleMessage;
use Playwright\Playwright;
$context = Playwright::chromium();
$page = $context->newPage();
$page->events()->onConsole(static function (ConsoleMessage $message): void {
$location = $message->location();
$lineNumber = isset($location['lineNumber']) ? (int) $location['lineNumber'] : null;
$suffix = null !== $lineNumber ? sprintf(' (line %d)', $lineNumber) : '';
printf('[%s] %s%s'.PHP_EOL, strtoupper($message->type()), $message->text(), $suffix);
});
$html = <<<'HTML'
<!DOCTYPE html>
<html lang="en">
<body>
<script>
console.log('Hello from the page console');
console.warn('Heads up! This is a warning message.');
console.error('Something went wrong.');
</script>
</body>
</html>
HTML;
$page->goto('data:text/html,'.rawurlencode($html));
// Trigger an additional console message after navigation.
$page->evaluate("() => console.info('Additional info message from evaluate()')");
usleep(250000);
$page->close();
$context->close();