How to install Playwright
To install Playwright and set it up for your project, follow these steps:
1. Install Playwright
You can install Playwright via npm, which is the recommended way:
npm install playwright
This command installs Playwright and its dependencies locally in your project.
2. Choose a Browser
Playwright supports multiple browsers (Chromium, Firefox, WebKit). You need to specify which browser(s) you want to use. You can install the browser binaries separately or let Playwright manage them for you.
Install Specific Browser Binaries
For example, to install Chromium:
npx playwright install chromium
Let Playwright Manage Browsers (Recommended)
Playwright can manage browser binaries for you based on your platform and Playwright version. It downloads and caches necessary browser versions automatically.
npx playwright install
This command installs all supported browsers (Chromium, Firefox, WebKit) suitable for your operating system.
3. Set Up Playwright in Your Project
Once installed, you can start using Playwright in your project. Here's a basic example of how to use Playwright in JavaScript:
const { chromium, firefox, webkit } = require('playwright');
(async () => {
// Launch a browser instance
const browser = await chromium.launch();
// Create a browser context
const context = await browser.newContext();
// Create a page
const page = await context.newPage();
// Navigate to a URL
await page.goto('https://example.com');
// Perform actions on the page
// e.g., interact with elements, take screenshots, etc.
// Close the browser
await browser.close();
})();