Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created September 27, 2024 12:15
Show Gist options
  • Save imshaiknasir/f8c60d8f61485cd8d47e1b790fde64d5 to your computer and use it in GitHub Desktop.
Save imshaiknasir/f8c60d8f61485cd8d47e1b790fde64d5 to your computer and use it in GitHub Desktop.
Existing Chrome Session with Playwright

Existing Chrome Session with Playwright

Connecting to an Existing Chrome Session with Playwright: A Comprehensive Guide

In the ever-evolving world of web development, automation is a game-changer. Whether you're a developer, tester, or tech enthusiast, automating browser tasks can significantly enhance productivity. One powerful tool for browser automation is Playwright, a versatile framework that supports multiple browsers and languages. In this guide, we'll delve into how to connect to an existing Chrome session using Playwright and execute some basic tasks.

Step 1: Launch Chrome with Remote Debugging

To connect Playwright to a running Chrome session, you need to start Chrome with remote debugging enabled. This setup allows Playwright to communicate with the browser via a debugging port.

# Start Chrome with remote debugging~ google-chrome --remote-debugging-port=9222

By executing this command, you initiate Chrome with remote debugging on port 9222, setting the stage for seamless interaction with Playwright.

Step 2: Connect Playwright to Your Browser

With Chrome up and running, it's time to harness Playwright's power to connect and automate tasks:

const { chromium } = require("playwright");

(async () => {
  // Connect to the running browser using the debugging port
  const browser = await chromium.connectOverCDP("http://localhost:9222");
  const context = browser.contexts()[0];

  // Open a new page in the connected browser
  const pages = context.pages();
  const page = pages.length > 0 ? pages[0] : await context.newPage();

  // Navigate to a chosen website
  await page.goto("https://example.com");
  const title = await page.title();
  console.log(`Page title: ${title}`);

  // Close the browser connection
  // await browser.close();
})();

Key Highlights:

  • Seamless Connection: Utilize chromium.connectOverCDP() to link Playwright with your active Chrome session.
  • Browser Contexts: Access isolated environments within your browser, akin to incognito windows.
  • Efficient Navigation: Open or create pages and navigate effortlessly while retrieving essential information like page titles.

Step 3: Graceful Disconnection

While our example keeps the connection open for demonstration purposes, it's best practice to close connections when they're no longer needed:

// Close the browser connection
// await browser.close();

Uncommenting this line ensures that your script tidily wraps up by closing the connection.

Conclusion: Elevate Your Testing with Playwright

Playwright empowers you to automate browsing tasks efficiently across major browsers like Chrome, Firefox, and Safari. Its robust features—such as automatic waiting for elements, parallel testing capabilities, and seamless CI/CD integration—make it an indispensable tool for modern web testing. Embrace Playwright's potential to streamline your workflows and enhance productivity in your development projects. Happy automating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment