-
Notifications
You must be signed in to change notification settings - Fork 266
Patchright compatibility #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Patchright compatibility #1021
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6bef9e0
to
2759cf2
Compare
🟠 dump.jsThe result is flaky, sometimes ok, sometimes err.
// Import the Chromium browser into our scraper.
import { chromium } from 'patchright';
// browserAddress
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
// web serveur url
const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234';
// measure general time.
const gstart = process.hrtime.bigint();
// store all run durations
let metrics = [];
// Connect to an existing browser
console.log("Connection to browser on " + browserAddress);
const browser = await chromium.connectOverCDP({
endpointURL: browserAddress,
logger: {
isEnabled: (name, severity) => true,
log: (name, severity, message, args) => console.log(`${name} ${message}`)
}
});
const context = await browser.newContext({
baseURL: baseURL,
});
const page = await context.newPage();
await page.goto('/campfire-commerce/');
const html = await page.content();
if (html.substring(0, 20) !== "<!DOCTYPE html><html") {
console.log(html.substring(0, 20));
throw new Error("html content is not as expected");
}
await page.close();
await context.close();
// Turn off the browser to clean up after ourselves.
await browser.close(); |
❌ cdp.js// Import the Chromium browser into our scraper.
import { chromium } from 'patchright';
// browserAddress
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
// web serveur url
const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234';
// runs
const runs = process.env.RUNS ? parseInt(process.env.RUNS) : 100;
// measure general time.
const gstart = process.hrtime.bigint();
// store all run durations
let metrics = [];
// Connect to an existing browser
console.log("Connection to browser on " + browserAddress);
const browser = await chromium.connectOverCDP(browserAddress);
for (var run = 0; run<runs; run++) {
// measure run time.
const rstart = process.hrtime.bigint();
const context = await browser.newContext({
baseURL: baseURL,
});
const page = await context.newPage();
await page.goto('/campfire-commerce/');
// ensure the price is loaded.
await page.waitForFunction(() => {
const price = document.querySelector('#product-price');
return price.textContent.length > 0;
}, {}, {timeout: 100}); // timeout 100ms
// ensure the reviews are loaded.
await page.waitForFunction(() => {
const reviews = document.querySelectorAll('#product-reviews > div');
return reviews.length > 0;
}, {}, {timeout: 100}); // timeout 100ms
let res = {};
res.name = await page.locator('#product-name').textContent();
res.price = parseFloat((await page.locator('#product-price').textContent()).substring(1));
res.description = await page.locator('#product-description').textContent();
res.features = await page.locator('#product-features > li').allTextContents();
res.image = await page.locator('#product-image').getAttribute('src');
let related = [];
var i = 0;
for (const row of await page.locator('#product-related > div').all()) {
related[i++] = {
name: await row.locator('h4').textContent(),
price: parseFloat((await row.locator('p').textContent()).substring(1)),
image: await row.locator('img').getAttribute('src'),
};
}
res.related = related;
let reviews = [];
var i =0;
for (const row of await page.locator('#product-reviews > div').all()) {
reviews[i++] = {
title: await row.locator('h4').textContent(),
text: await row.locator('p').textContent(),
};
}
res.reviews = reviews;
// console.log(res);
// assertions
if (res['price'] != 244.99) {
console.log(res);
throw new Error("invalid product price");
}
if (res['image'] != "images/nomad_000.jpg") {
console.log(res);
throw new Error("invalid product image");
}
if (res['related'].length != 3) {
console.log(res);
throw new Error("invalid products related length");
}
if (res['reviews'].length != 3) {
console.log(res);
throw new Error("invalid reviews length");
}
process.stderr.write('.');
if(run > 0 && run % 80 == 0) process.stderr.write('\n');
await page.close();
await context.close();
metrics[run] = process.hrtime.bigint() - rstart;
}
// Turn off the browser to clean up after ourselves.
await browser.close();
const gduration = process.hrtime.bigint() - gstart;
process.stderr.write('\n');
const avg = metrics.reduce((s, a) => s += a) / BigInt(metrics.length);
const min = metrics.reduce((s, a) => a < s ? a : s);
const max = metrics.reduce((s, a) => a > s ? a : s);
console.log('total runs', runs);
console.log('total duration (ms)', (gduration/1000000n).toString());
console.log('avg run duration (ms)', (avg/1000000n).toString());
console.log('min run duration (ms)', (min/1000000n).toString());
console.log('max run duration (ms)', (max/1000000n).toString()); |
karlseguin
reviewed
Sep 10, 2025
e0004b3
to
2fc033f
Compare
In this case we reuse the existing isolated world and isolated context and we log a warning
2fc033f
to
5d1e17c
Compare
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adjust CDP support to improve compatibility with patchright-nodejs client.
Some issues in progress
🟠
target.createBrowserContext
withdisposeOnDetach: true
parameterpatchright sends
disposeOnDetach: true
. We didn't implement it for now.ℹ️ It raises a warning.
https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-createBrowserContext
🟠
DOM.describeNode
withpierce: true
parameterpatchright sends
pierce: true
. We didn't implement it for now.ℹ️ I turned the error into a log warning.
https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-describeNode
✔️ Two successive
Page.createIsolatedWorld
callsRelates with https://github.com/lightpanda-io/project/discussions/163 for the risks of reusing the same world+context.
ℹ️ implemented in bc1ad98
❌ JS exection error
DispatchRequest
❌ JS execution error
DOMException\n at CRExecutionContext.evaluateWithArguments