-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Question] Trying to connect to existing playwright session via Chromium CDP #11442
Comments
Something like this might work for you: //@ts-check
import { chromium } from 'playwright';
(async () => {
const context1 = await chromium.launchPersistentContext('', {
args: ['--remote-debugging-port=9222'],
headless: false,
userAgent: 'Some Overriden User Agent',
});
await context1.addCookies([
{
name: 'Some cookie',
value: 'Some cookie value',
url: 'https://example.com',
},
]);
context1.addInitScript(() => window.hello = 'hello');
const pageOne = await context1.newPage();
await pageOne.goto('https://google.com');
const browser2 = await chromium.connectOverCDP('http://localhost:9222');
// Shows 1 context
console.log(
'Number of contexts in CDP browser session: ',
browser2.contexts().length
);
const contextTwo = browser2.contexts()[0];
// Shows 0 pages
console.log(
'Number of pages in CDP browser session: ',
contextTwo.pages().length
);
// Creating a new page blows up with error:
//
// browserContext.newPage: Cannot read property 'pageOrError' of undefined
// at file:///Users/oliverswitzer/workspace/playwright-cdp-spike/first_session.js:50:36 { name: 'TypeError' }
const pageTwo = await contextTwo.newPage();
await browser2.close();
await context1.close();
})(); |
@mxschmitt that totally works, thank you! It sounds like the magic sauce I was missing was Also, I noticed that Thank you! |
Yes! There is no difference, because we would do internally the same: playwright/packages/playwright-core/src/server/chromium/chromium.ts Lines 337 to 355 in 0d277fa
|
Closing since this issue seems answered. Please file a new issue for further questions. |
@mxschmitt This seems to still be an issue in the python library. Here is the script that shows the error
|
Hi,
I have a particular use case that involves starting a playwright Chromium session via node script, and connecting to that session via
chromium.connectOverCDP
via a playwright wrapper library from another language (I'm using playwright-elixir).The reason for me wanting to do this is to take advantage of node libraries that add on functionality to playwright, like Header rotation etc, while still using my preferred language to interact with playwright. I expect these libraries will make these modifications to the browse session:
My hope is that connected to said session via CDP will allow me to inherit those modifications made to the session by the playwright libraries I plan on using.
Here's an example of two of the libraries I was hoping to use:
puppeteer-extra
to playwright. Development is being tracked in this issue [Info] Beta versions available for the newpuppeteer-extra
&playwright-extra
berstend/puppeteer-extra#454I do not wish to re-implement these library behaviors in Elixir, and would much rather attach to an existing session that was started in node that has used one of these libraries.
I have been reading this previous issue that seems to state that you can use Chromium +
chromium.connectOverCDP
to connect to a pre-existing playwright session.I have attempted to re-create what this comment from @dmitrysteblyuk and this comment from @mxschmitt seem to suggest as the way to do this with the following script, but have run into a few issues.
The breakdown is:
So far I have no been able to get past step 5.
Here is the repo with the above script that you can use to reproduce this issue.
While I can see that there is a pre-existing browser context after connecting over CDP to the browserOne session, I do not see that there are any pre-existing pages on that context.
Also, when I try to create a new page with
await contextTwo.newPage();
on the context created via CDP I get the following error:In googling for the error that I received when created a newPage I found this old playwright issue thread from 2020. It claims that it is due to using too old of a version of Chrome. However, in this case I am using Chromium, and have also validated that my version is the expected version from playwright's perspective:
I have verified that I am using the pinned version by opening Chromium from my library cache
~/Library/Caches/ms-playwright/chromium-939194/chrome-mac/Chromium.app
and checking the version to compare to what is pinned in npm for playwright:Any insights on how to proceed would be greatly appreciated!
Thanks
The text was updated successfully, but these errors were encountered: