DEV Community

Sony AK
Sony AK

Posted on

8

How to use Playwright with external/existing Chrome

Mostly example using Playwright in JavaScript is using Chromium that bundled with Playwright. How about if we want to use existing or external Chrome?

No worries, it means you have to set the Playwright to connect to a Chromium instance manually using Chrome DevTools Protocol (CDP) and that means you should run the Chrome with --remote-debugging-port=9222.

For the external Chrome I recommend using Chrome for Testing that is good for this situation. Just to to https://developer.chrome.com/blog/chrome-for-testing to read more.

I have created the repository and quick script sample about this. Just go to https://github.com/sonyarianto/playwright-using-external-chrome

Wanna quick try? Here is the script index.js.

import { chromium } from 'playwright';

(async () => {
    try {
        const browser = await chromium.connectOverCDP('http://localhost:9222');

        console.log(browser.isConnected() && 'Connected to Chrome.');
        console.log(`Contexts in CDP session: ${browser.contexts().length}.`);

        const context = browser.contexts()[0];

        const page = await context.newPage();
        await page.goto('https://example.com');
        await page.screenshot({ path: 'example.png' });

        await page.close();
        await context.close();
        await browser.close();
    } catch (error) {
        console.log('Cannot connect to Chrome.');
    }
})();
Enter fullscreen mode Exit fullscreen mode

Then run it like this. Make sure you already run the Chrome first (with argument --remote-debugging-port=9222).

node ./index.js
Enter fullscreen mode Exit fullscreen mode

Happy using Playwright.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (3)

Collapse
 
akhil_charugulla_896edc6c profile image

I am getting this error while running the code. Please can you help what is going wrong
Running chrome in remote debugging port 9223

Image description

PS B:\job-search\jobsearchplaywright> npx ts-node excelToGoogleSearch.ts
node:internal/modules/cjs/loader:1228
throw err;
^

Error: Cannot find module './excelToGoogleSearch.ts'
Require stack:

  • B:\job-search\jobsearchplaywright\imaginaryUncacheableRequireResolveScript at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15) at Function.resolve (node:internal/modules/helpers:190:19) at requireResolveNonCached (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:549:16) at getProjectSearchDir (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:519:40) at phase3 (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:267:27) at bootstrap (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:47:30) at main (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:33:12) at Object. (B:\job-search\jobsearchplaywright\node_modules\ts-node\dist\bin.js:579:5) at Module._compile (node:internal/modules/cjs/loader:1469:14) at Module._extensions..js (node:internal/modules/cjs/loader:1548:10) { code: 'MODULE_NOT_FOUND', requireStack: [ 'B:\job-search\jobsearchplaywright\imaginaryUncacheableRequireResolveScript' ] }
Collapse
 
akhil_charugulla_896edc6c profile image
akhil charugulla

solved it
its just a missing import issue

Collapse
 
vlatkodimeski profile image
driveIN

good job man!
Thanks!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more