The following script is an example showing how to utilize Smartproxy with Playwright. Check out the official Playwright repository for more information on development using this tool.
To run the example script, you'll need to install the following:
Follow these steps to set up and get started right away:
-
Install Playwright. You can get Playwright using npm, yarn, or pnpm by entering the command below into your terminal. You'll have a few prompts to answer, such as picking between TypeScript and JavaScript, the name of your tests folder, and browsers:
npm:
npm init playwright@latest
yarn:
yarn create playwright
pnpm:
pnpm create playwright
-
Include Playwright in your script. Create a new JavaScript (.js) file and include the following line at the beginning. You can switch the chromium option with webkit or firefox if you have them installed:
const { chromium } = require('playwright');
- Navigate to a web page and extract a specific element. For this example, we're going to extract data from a website called ScrapeMe, which is ideal for various web scraping tests. The website has a list of items similar to those of a regular online shop. While Playwright offers a wide range of features to interact with web pages, for this example, we'll simply select the 3rd product from the list by its class name. If you're unsure how to inspect a website's HTML and find the class name of the titles, check out our comprehensive guide on inspecting elements.
const { chromium } = require('playwright');
(async () => {
// Launch a new browser instance
const browser = await chromium.launch({ headless: false });
// Open a new page
const page = await browser.newPage();
// Navigate to the ScrapeMe website
await page.goto('https://scrapeme.live/shop/');
// Select all elements matching the class
const productElements = await page.$$('.woocommerce-loop-product__title');
// Access the 3rd element (index 2) and get its text content
const thirdProductTitle = await productElements[2].textContent();
console.log(`3rd Product Title: ${thirdProductTitle}`);
// After the above actions are performed, close the browser.
await browser.close();
})();
- Proxy implementation. To use proxies with Playwright, you can pass proxy settings through the browser's launch or launchPersistentContext options. Playwright supports proxy integration via the proxy object, which accepts the proxy server URL. See the full file here.
- Run the script. While in your project folder, run the following command:
node playwright.js
This script does several things – first, it connects to a proxy server to make future requests through a different IP address. Then, it makes a request to the Smartproxy IP-checker website to print your IP address to check if the connection is coming from a different address from your own. Finally, it makes the same request to the ScrapeMe website that prints the 3rd element from the product page.
If you did everything correctly, you should see the following result in your terminal:
Email - [email protected]
Live chat 24/7