Prasit Mankad

The CDP pattern: automating any website you are already logged into

Chrome DevTools Protocol lets Playwright reuse your existing authenticated browser session. Here is why that matters and how to use it.

The hard part of browser automation isn’t writing the code. It’s authentication.

Most automation tools launch a fresh browser instance. That means no cookies, no session tokens, no saved state. You have to replay the login, handle 2FA, deal with CAPTCHA, and hope the site doesn’t flag the new session as suspicious. For sites with aggressive bot detection, this is often impossible.

The Chrome DevTools Protocol solves this by connecting to a browser that’s already running, already logged in, already past all of that.

How it works

Launch Chromium or Brave with a remote debugging port:

/usr/lib/chromium/chromium --remote-debugging-port=9222 --profile-directory=Default &

Then in Python, connect Playwright to that running instance:

from playwright.async_api import async_playwright

async with async_playwright() as p:
    browser = await p.chromium.connect_over_cdp("http://localhost:9222")
    context = browser.contexts[0]  # reuse existing session
    page = context.pages[0]        # or open a new tab in the existing context

The critical detail is browser.contexts[0]. If you call browser.new_context() instead, Playwright creates a fresh, unauthenticated session. You lose everything. contexts[0] gives you the existing context with all its cookies and session state intact. This is the one thing that makes the pattern work.

What I’ve used it for

I extracted 149 Gemini chat conversations using this pattern. Google Takeout gives you metadata, not content. The actual conversation text isn’t in the export. So I wrote a script that opened each conversation URL in my existing authenticated Chrome session, waited for the content to load, and scraped the text. It took about 20 minutes to run and got everything.

LinkedIn connection management is another case. LinkedIn’s personal account API is deliberately limited. You can’t programmatically manage your connections through any official channel. But you can automate your own browser session to do things you’d otherwise do manually, at a pace that doesn’t trigger rate limiting.

The Proton Mail automation I described in a previous post also uses this pattern, though in that case I’m going through Proton Bridge’s IMAP interface rather than the web UI.

The ToS question

This is technically against the terms of service for most platforms. I want to be direct about that rather than glossing over it.

My ethical frame: I’m automating my own session, accessing my own data, on my own machine. I’m not scraping public data at scale. I’m not building a product on top of someone else’s platform. I’m extracting content I created, from an account I own, because the platform doesn’t provide an adequate export mechanism.

Google’s Takeout not including Gemini conversation content is a choice. LinkedIn’s API restrictions are a choice. When platforms make it difficult to access your own data, the CDP pattern is a practical response. Whether that justifies the ToS violation is a judgment call each person has to make for themselves.

The automation trap

xkcd 1319 is called “Automation.” The joke is that automating a task takes longer than just doing it manually. The chart shows a task that takes 5 minutes manually, and the automation effort expanding to fill weeks.

The CDP pattern is the exception to this. Once you have the basic connection code working, it’s reusable. The Gemini extraction script took maybe two hours to write. Running it manually would have taken days. The automation paid off immediately, and the pattern transfers to the next site with minimal changes.

The reason it transfers is that the authentication problem is solved once. You’re not writing new auth logic for each site. You’re reusing your existing session. The site-specific work is just finding the right selectors and handling the page structure, which is the part that varies anyway.

A note on hidden inputs

One thing that trips people up with CDP automation: many sites implement custom toggle switches where the actual <input> element is CSS-hidden, and a styled <label> is what the user visually clicks. Playwright’s standard click() will time out on the hidden input because it checks that the element is visible before clicking.

The fix is dispatch_event('click'), which fires the event directly without actionability checks:

await page.locator('input[role="switch"]').dispatch_event('click')

Also avoid set_checked() on custom toggles. There’s a known Playwright bug where set_checked(True) disables the toggle and set_checked(False) enables it. The behaviour is inverted. Use dispatch_event('click') and then verify the state by reading the aria-checked attribute.

What this unlocks

The CDP pattern works for any site where you have an account but no API. That’s most sites. The web is full of data you’ve created, conversations you’ve had, content you’ve saved, that platforms don’t make easy to export. The CDP pattern is a practical way to get it back.

It’s not a general-purpose scraping tool. It’s specifically useful for your own data, in your own authenticated sessions, on your own machine. That scope is narrower than general web scraping, but it covers a lot of genuinely useful cases.