Skip to main content

Console & Eval

Console messages

playwright-cli console # all messages (info and above)
playwright-cli console error # errors only
playwright-cli console warning # warnings and errors
playwright-cli console debug # everything
playwright-cli console --clear # clear the message buffer

Each level includes the messages of more severe levels; the default is info. The output starts with a count line, so you can tell at a glance whether a page is clean:

$ playwright-cli console error
# Total messages: 37 (Errors: 2, Warnings: 5)
# Returning 2 messages for level "error"
#
# [ERROR] Uncaught TypeError: Cannot read property 'map' of undefined @ app.js:42
# [ERROR] Failed to load resource: 404 (Not Found) @ /api/users

Workflow: debugging a broken page

# Check the console for errors
playwright-cli console error
# [ERROR] Failed to fetch: GET https://api.example.com/data 404

# Now you know the API endpoint is returning 404
# Mock the route or investigate further
playwright-cli route "**/api/data" \
--body='{"items":[]}' --content-type=application/json
playwright-cli reload

JavaScript evaluation

playwright-cli eval <func> [target]

The argument is a function — () => { ... } for the page, element => { ... } when a target is given. Pass --filename to write the result to a file instead of returning it inline.

Page-level evaluation

$ playwright-cli eval "() => document.title"
# React - TodoMVC

$ playwright-cli eval "() => window.innerWidth + 'x' + window.innerHeight"
# 1280x720

$ playwright-cli eval "() => JSON.stringify([...document.querySelectorAll('a')].map(a => a.href))" \
--filename=links.json

Element evaluation

eval is the way to read the attributes a snapshot doesn't show — id, class, data-*, computed styles:

$ playwright-cli eval "el => el.id" e15
# item-42

$ playwright-cli eval "el => el.getAttribute('data-testid')" e15
# todo-item

$ playwright-cli eval "el => getComputedStyle(el).color" e5
# rgb(255, 0, 0)

Piping results

--raw strips the page status and snapshot, leaving only the value:

playwright-cli --raw eval "JSON.stringify(performance.timing)" | jq '.loadEventEnd - .navigationStart'

Running Playwright code

Execute arbitrary Playwright scripts with full API access:

playwright-cli run-code <code>
playwright-cli run-code --filename=script.js

The code must be a single function expression — it is wrapped in (...) and evaluated, so import / require are not available. It receives the current page, and whatever it returns is printed.

Set geolocation

playwright-cli run-code "async (page) => {
await page.context().grantPermissions(['geolocation']);
await page.context().setGeolocation({latitude: 37.77, longitude: -122.42});
}"

Wait for a specific condition

playwright-cli run-code "async (page) => {
await page.waitForSelector('.data-loaded');
return 'Data loaded successfully';
}"

Emulate media

playwright-cli run-code "async (page) => page.emulateMedia({ colorScheme: 'dark' })"

Work inside an iframe

playwright-cli run-code "async (page) => {
const frame = page.locator('iframe#checkout').contentFrame();
await frame.getByRole('button', { name: 'Pay' }).click();
}"

Scrape structured data

playwright-cli run-code "async (page) => {
const items = await page.$$eval('.product', els =>
els.map(el => ({
name: el.querySelector('.name').textContent,
price: el.querySelector('.price').textContent
}))
);
return JSON.stringify(items, null, 2);
}"