browser
browser copied to clipboard
Support Playwright locators
My code:
console.log(` Navigating to ${url}...`);
await page.goto(url, { waitUntil: 'networkidle', timeout: 5000 }); // Increased timeout
console.log(` Navigation successful.`);
console.log(` Extracting body text...`);
const text = await page.evaluate(() => {
// Use DOM API to extract text
const body = document.querySelector('body');
return body ? body.innerText : '';
});
// const text = await page.locator('body').innerText();
console.log(` Text extracted (${text.length} characters).`);
console.log(` Saving text to ${textPath}...`);
fs.writeFileSync(textPath, text);
console.log(` Text file saved.`);
Do you support Playwright locators? When I tried using const text = await page.locator('body').innerText(); I got:
Error processing https://www.forbes.com/lists/largest-private-companies: locator.innerText: Target page, context or browser has been closed
Call log:
- waiting for locator('body')
at <anonymous> (/Users/al/c2/m2/test-lightpanda/screenshotter.ts:170:49)
Closing page for https://www.forbes.com/lists/largest-private-companies...
Closing context for https://www.forbes.com/lists/largest-private-companies...
Hello @alexkreidler,
We have multiples issues currently with Playwright, but we plan to focus on it the coming weeks.
Hello @alexkreidler, We make progress with Playwright support recently. Can you make a try?
I tested
import { chromium } from 'playwright';
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
const browser = await chromium.connectOverCDP(browserAddress);
const context = await browser.newContext();
const page = await context.newPage();
const url = 'https://example.com'
console.log(` Navigating to ${url}...`);
await page.goto(url, { waitUntil: 'networkidle', timeout: 5000 }); // Increased timeout
console.log(` Navigation successful.`);
console.log(` Extracting body text...`);
const text = await page.locator('body').innerText();
console.log(` Text extracted (${text.length} characters).`);
await page.close();
await context.close();
await browser.close();
I get a good result:
$ node playwright/locators.js
Navigating to https://example.com...
Navigation successful.
Extracting body text...
Text extracted (209 characters).