Connect to an Existing Browser¶
If Chrome is already running with remote debugging enabled, connect to it instead of launching a new process.
Start Chrome manually¶
Or on Windows:
Connect with cdpwave¶
CDPClient.connect uses HTTP discovery (/json/version) to find the WebSocket URL. No browser process is managed — closing the client only closes the WebSocket, not the browser.
List and connect to pages¶
pages = await client.get_pages()
for target in pages:
print(f"{target.target_id} | {target.title} | {target.url}")
if pages:
session = await client.connect_to_page(pages[0].target_id)
result = await session.runtime.evaluate("document.title", return_by_value=True)
print(result["result"]["value"])
await session.close()
Full example¶
import asyncio
from cdpwave import CDPClient
async def main() -> None:
async with await CDPClient.connect(host="localhost", port=9222) as client:
pages = await client.get_pages()
for t in pages:
print(f" {t.target_id} | {t.type} | {t.title} | {t.url}")
if pages:
session = await client.connect_to_page(pages[0].target_id)
result = await session.runtime.evaluate("document.title", return_by_value=True)
print(f"Title: {result['result']['value']}")
await session.close()
asyncio.run(main())