Use a dedicated Chrome user-data-dir and attach Playwright only after Chrome is already running.
Quick Start
- Run scripts/launch_logged_in_chrome.ps1 to start normal Chrome with a dedicated profile folder and a CDP port. The script now waits for the dedicated-profile Chrome process and the CDP endpoint before it reports success.
- Let the user log in manually if the profile is not logged in yet.
- In
js_repl, attach Playwright withchromium.connectOverCDP("http://127.0.0.1:<port>"). - Reuse the attached browser and page handles for the rest of the task.
Defaults
- Default user-data-dir:
D:\Prj\onizuka-playwright-profile - Default CDP port:
9222 - Default login URL:
https://accounts.google.com/
Rules
- Do not log into Google from a Playwright-launched Chrome window.
- Do not point Playwright at
%LOCALAPPDATA%\Google\Chrome\User Data. - Do use a dedicated user-data-dir that is separate from the user's everyday Chrome profile.
- Do launch Chrome first as a normal browser, then attach Playwright over CDP.
- Do keep the attached CDP browser alive across steps instead of reconnecting unless the process changed.
Launch
Run:
powershell -ExecutionPolicy Bypass -File .\scripts\launch_logged_in_chrome.ps1
Do not assume launch succeeded just because a command returned. Only continue when the launch script exits successfully or scripts/check_cdp_port.ps1 confirms the CDP endpoint.
If a stale browser is already using the same profile folder, close it first:
powershell -ExecutionPolicy Bypass -File .\scripts\close_logged_in_chrome.ps1
Attach From js_repl
Use a local workspace that already has playwright or playwright-core installed, then attach:
var chromium;
var attachedBrowser;
var attachedContext;
var attachedPage;
{
const nm = await import("node:module");
const requireForPw = nm.createRequire("file:///D:/Prj/demo/package.json");
({ chromium } = requireForPw("playwright-core"));
attachedBrowser = await chromium.connectOverCDP("http://127.0.0.1:9222");
attachedContext = attachedBrowser.contexts()[0];
attachedPage = attachedContext.pages()[0];
}
After attaching, navigate and automate normally.
Verify Before Automating
- Confirm the browser window is the dedicated profile, not the user's main Chrome.
- Confirm the user has finished manual login if the task depends on Google auth.
- Confirm the CDP endpoint is reachable by running scripts/check_cdp_port.ps1 before attaching if launch behavior looked suspicious.
- If launch times out or the dedicated-profile
chrome.exeprocess does not appear, stop and relaunch instead of trying to attach anyway.
Troubleshooting
Read references/troubleshooting.md when:
- Google shows "This browser or app may not be secure"
connectOverCDP()fails- the CDP endpoint is not reachable
- the launch script returns but no Chrome process is using the dedicated profile
- a stale profile lock prevents launch