Playwright Examples for Web Scraping and Automation

Playwright Examples for Web Scraping and Automation


Playwright is a versatile Node.js library for automating Chromium, WebKit, and Firefox browsers with a unified API. It supports multiple languages, including JavaScript, Python, C#, and Java, enabling fast and reliable cross-browser automation.

In this article, we focus on practical examples to demonstrate how Playwright can be effectively used with Python and JavaScript.



Setup

Before diving into Playwright’s automation capabilities, it’s essential to set up the environment correctly. You can install it using npm (for JavaScript) or pip (for Python).



JavaScript

$ npm install playwright
Enter fullscreen mode

Exit fullscreen mode



Python

$ pip install playwright
Enter fullscreen mode

Exit fullscreen mode

After installing the Playwright library, it’s recommended to install the browser binaries for the browsers you intend to automate. You can do this by running the following commands:



JavaScript

npx playwright install
Enter fullscreen mode

Exit fullscreen mode



Python

playwright install
Enter fullscreen mode

Exit fullscreen mode

With the setup complete, you’re ready to begin automating browsers using Playwright. Let’s dive into some practical examples!



Form submission

Automating form submissions is a common task in web automation. In this example, we will automate the submission of a form using Playwright.



JavaScript

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

(async () => {
  // Launch a new Chromium browser instance
  const browser = await chromium.launch();
  // Create a new browser page
  const page = await browser.newPage();
  // Navigate to the login page
  await page.goto("https://web-scraping.dev/login");

  // Fill in the username field
  await page.fill('input[name="username"]', "user123");
  // Fill in the password field
  await page.fill('input[name="password"]', "password");
  // Click the submit button to log in
  await page.click('button[type="submit"]');

  // Wait for the URL to change, indicating a successful login
  await page.waitForURL("https://web-scraping.dev/login");
  console.log("Login successful");

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch a new Chromium browser instance
    browser = p.chromium.launch()
    # Create a new browser page
    page = browser.new_page()
    # Navigate to the login page
    page.goto('https://web-scraping.dev/login')

    # Fill in the username field
    page.fill('input[name="username"]', 'user123')
    # Fill in the password field
    page.fill('input[name="password"]', 'password')
    # Click the submit button to log in
    page.click('button[type="submit"]')

    # Wait for the URL to change, indicating a successful login
    page.wait_for_url('**/login')
    print('Login successful')

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This example demonstrates how to automate the login process on a website. The script navigates to the login page, fills in the username and password fields, clicks the submit button, and waits for the URL to confirm a successful login. Upon success.



Capture Local Storage

Accessing and manipulating local storage is crucial when dealing with client-side data. In this example, we will capture the local storage of a website using Playwright.



JavaScript

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

(async () => {
  // Launch a new Chromium browser instance
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the page that utilizes local storage
  await page.goto("https://web-scraping.dev/local-storage");

  // Set a key-value pair in local storage using JavaScript within the page context
  await page.evaluate(() => {
    localStorage.setItem("name", "John Doe");
  });

  // Retrieve all items from local storage
  const localStorageData = await page.evaluate(() => {
    return { ...localStorage };
  });
  console.log(localStorageData);

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch a new Chromium browser instance
    browser = p.chromium.launch()
    page = browser.new_page()
    # Navigate to the page that utilizes local storage
    page.goto('https://web-scraping.dev/local-storage')

    # Set a key-value pair in local storage using JavaScript within the page context
    page.evaluate('() => { localStorage.setItem("name", "John Doe"); }')

    # Retrieve all items from local storage
    local_storage = page.evaluate('() => { return { ...localStorage }; }')
    print(local_storage)

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This example shows how to interact with the browser’s local storage. The script sets a new item in local storage and then retrieves all local storage data to verify the operation. The evaluate function allows running JavaScript code within the context of the page.



Capture Cookies

Cookies play an essential role in session management and authentication, In this example, we will capture the cookies of a website using Playwright.



JavaScript

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

(async () => {
  // Launch a new Chromium browser instance
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the login page
  await page.goto("https://web-scraping.dev/login");

  // Fill in the username and password fields
  await page.fill('input[name="username"]', "user123");
  await page.fill('input[name="password"]', "password");
  // Click the submit button to log in
  await page.click('button[type="submit"]');

  // Wait for the URL to confirm the login has been processed
  await page.waitForURL("https://web-scraping.dev/login");
  console.log("Login successful");

  // Capture cookies from the current browser context
  const cookies = await page.context().cookies();
  console.log(cookies);

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch a new Chromium browser instance
    browser = p.chromium.launch()
    page = browser.new_page()
    # Navigate to the login page
    page.goto("https://web-scraping.dev/login")

    # Fill in the username and password fields
    page.fill('input[name="username"]', "user123")
    page.fill('input[name="password"]', "password")
    # Click the submit button to log in
    page.click('button[type="submit"]')

    # Wait for the URL to confirm the login has been processed
    page.wait_for_url("https://web-scraping.dev/login")
    print("Login successful")

    # Capture cookies from the current browser context
    cookies = page.context.cookies()
    print(cookies)

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

After logging into the website, this script captures all cookies associated with the current browser context. This is particularly useful for session management, authentication, and maintaining state across different browser instances.



Reuse Login Session

Avoid logging in repeatedly by saving and reusing login sessions, In this example, we will reuse the login session of a website using Playwright by saving and loading cookies.



JavaScript

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

(async () => {
  // First browser instance
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the login page
  await page.goto("https://web-scraping.dev/login");

  // Perform login by filling in credentials
  await page.fill('input[name="username"]', "user123");
  await page.fill('input[name="password"]', "password");
  await page.click('button[type="submit"]');

  // Wait for the URL to confirm the login has been processed
  await page.waitForURL("https://web-scraping.dev/login");
  console.log("Login successful");

  // Save cookies from the first session
  const cookies = await page.context().cookies();
  await browser.close();

  // New browser instance with saved cookies
  const newBrowser = await chromium.launch();
  const context = await newBrowser.newContext();
  // Add the saved cookies to the new browser context
  await context.addCookies(cookies);

  // Create a new page in the new context
  const newPage = await context.newPage();
  // Navigate to the homepage or any other page that requires authentication
  await newPage.goto("https://web-scraping.dev/");

  // Wait for an element that confirms the user is logged in
  await newPage.waitForSelector('a:has-text("logout")');
  console.log("Logout button found, session reused successfully");

  // Close the new browser instance
  await newBrowser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # First browser instance
    browser = p.chromium.launch()
    page = browser.new_page()
    # Navigate to the login page
    page.goto("https://web-scraping.dev/login")

    # Perform login by filling in credentials
    page.fill('input[name="username"]', "user123")
    page.fill('input[name="password"]', "password")
    page.click('button[type="submit"]')

    # Wait for the URL to confirm the login has been processed
    page.wait_for_url("https://web-scraping.dev/login")
    print("Login successful")

    # Save cookies from the first session
    cookies = page.context.cookies()
    browser.close()

    # New browser instance with saved cookies
    new_browser = p.chromium.launch()
    context = new_browser.new_context()
    # Add the saved cookies to the new browser context
    context.add_cookies(cookies)

    # Create a new page in the new context
    new_page = context.new_page()
    # Navigate to the homepage or any other page that requires authentication
    new_page.goto("https://web-scraping.dev/")

    # Wait for an element that confirms the user is logged in
    new_page.wait_for_selector('a:has-text("logout")')
    print("Logout button found, session reused successfully")

    # Close the new browser instance
    new_browser.close()
Enter fullscreen mode

Exit fullscreen mode

This example demonstrates how to persist a login session by saving cookies from an authenticated session and loading them into a new browser context. This approach allows you to maintain the logged-in state without having to log in again, which is useful for running multiple tests or scripts that require authentication.



Wait Element to load

Waiting for elements to load is critical for ensuring reliable automation, In this example, we will wait for an element to load on a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the target webpage
  await page.goto("https://web-scraping.dev/");

  // Wait for the h1 element to be visible on the page
  await page.waitForSelector("h1");

  // Retrieve the text content of the h1 element
  const title = await page.textContent("h1");
  console.log(title);

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = browser.new_page()
    # Navigate to the target webpage
    page.goto('https://web-scraping.dev/')

    # Wait for the h1 element to be visible on the page
    page.wait_for_selector('h1')

    # Retrieve the text content of the h1 element
    title = page.text_content('h1')
    print(title)

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This script navigates to a webpage and waits until the <h1> element is loaded and visible. Once the element is present, it retrieves and prints its text content. Waiting for elements to load is crucial to ensure that subsequent actions interact with fully rendered content.



Taking Screenshot

Screenshots are invaluable for debugging, visual validation, or documentation, In this example, we will take a screenshot of a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the target webpage
  await page.goto("https://web-scraping.dev");

  // Take a screenshot and save it as "screenshot.png"
  await page.screenshot({ path: "screenshot.png" });

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = browser.new_page()
    # Navigate to the target webpage
    page.goto('https://web-scraping.dev')

    # Take a screenshot and save it as "screenshot.png"
    page.screenshot(path='screenshot.png')

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This script navigates to a specified webpage and captures a screenshot of the visible area, saving it as screenshot.png. Taking screenshots is useful for debugging, documentation, or verifying the visual state of a webpage.



Convert Page to PDF

Need a printable version of a web page? Playwright can generate PDF files from web pages, In this example, we will convert a page to PDF using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the target webpage
  await page.goto("https://web-scraping.dev");

  // Generate a PDF of the current page and save it as "page.pdf"
  await page.pdf({ path: "page.pdf" });

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the target webpage
    page.goto('https://web-scraping.dev')

    # Generate a PDF of the current page and save it as "page.pdf"
    page.pdf(path='page.pdf')

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This script navigates to a webpage and converts the entire page into a PDF file named page.pdf. This feature is useful for generating printable versions of web pages or for archival purposes.



Handle File Download

Downloading files during automation is straightforward with Playwright, In this example, we will handle file downloads using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the download page
  await page.goto("https://web-scraping.dev/download");

  // Click the link or button that initiates the download
  await page.click("a");

  // Wait for the 'download' event to occur
  const download = await page.waitForEvent("download");

  // Save the downloaded file as "file.txt"
  await download.saveAs("file.txt");
  console.log("File downloaded successfully");

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the download page
    page.goto('https://web-scraping.dev/download')

    # Click the link or button that initiates the download
    page.click("a")

    # Wait for the 'download' event to occur
    download = page.wait_for_event('download')

    # Save the downloaded file as "file.txt"
    download.save_as('file.txt')
    print("File downloaded successfully")

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

When you interact with elements that trigger file downloads (like clicking a download link or button), Playwright emits a download event. The script listens for this event, waits for the download to complete, and then saves the file to the specified location.



Handle File Upload

File uploads are often part of web interactions, In this example, we will handle file uploads using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the file upload page
  await page.goto("https://web-scraping.dev/upload");

  // Select the file input element
  const input = await page.$('input[type="file"]');
  // Upload the file "file.txt" by setting it to the input element
  await input.setInputFiles("file.txt");

  // Click the submit button to upload the file
  await page.click('button[type="submit"]');
  // Wait for navigation to confirm the upload has been processed
  await page.waitForNavigation();
  console.log("File uploaded successfully");

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the file upload page
    page.goto('https://web-scraping.dev/upload')

    # Select the file input element
    input = page.query_selector('input[type="file"]')
    # Upload the file "file.txt" by setting it to the input element
    input.set_input_files('file.txt')

    # Click the submit button to upload the file
    page.click('button[type="submit"]')
    # Wait for navigation to confirm the upload has been processed
    page.wait_for_navigation()
    print('File uploaded successfully')

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This script automates the process of uploading a file to a website. It locates the file input element, sets the desired file (file.txt) for upload, submits the form, and waits for the navigation to confirm that the upload has been completed successfully.



Close Modal and Cookie Popup

Dealing with modals and cookie popups can interrupt automated workflows, In this example, we will close a modal and cookie popup using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the login page with cookies prompt
  await page.goto("https://web-scraping.dev/login?cookies");

  // Option #1: Attempt to click the "Accept Cookies" button
  try {
    await page.click("#cookie-ok", { timeout: 2000 });
    console.log("Cookie popup closed by clicking the button");
  } catch (error) {
    console.log("No cookie popup found or button not clickable");
  }

  // Option #2: Manually remove the modal elements from the DOM
  const cookieModal = await page.$("#cookieModal");
  if (cookieModal) {
    await cookieModal.evaluate((el) => el.remove());
    console.log("Cookie modal removed from DOM");
  }

  const modalBackdrop = await page.$(".modal-backdrop");
  if (modalBackdrop) {
    await modalBackdrop.evaluate((el) => el.remove());
    console.log("Modal backdrop removed from DOM");
  }

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright, TimeoutError

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the login page with cookies prompt
    page.goto("https://web-scraping.dev/login?cookies")

    # Option #1: Attempt to click the "Accept Cookies" button
    try:
        page.click("#cookie-ok", timeout=2000)
        print("Cookie popup closed by clicking the button")
    except TimeoutError:
        print("No cookie popup found or button not clickable")

    # Option #2: Manually remove the modal elements from the DOM
    cookie_modal = page.query_selector("#cookieModal")
    if cookie_modal:
        cookie_modal.evaluate("el => el.remove()")
        print("Cookie modal removed from DOM")

    modal_backdrop = page.query_selector(".modal-backdrop")
    if modal_backdrop:
        modal_backdrop.evaluate("el => el.remove()")
        print("Modal backdrop removed from DOM")

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

Websites often display modals or cookie consent popups that can interfere with automation scripts. This example provides two approaches to handle such popups:

  1. Clicking the Close Button: Attempts to click the button that dismisses the popup. If the button isn’t found within the specified timeout, it catches the error.
  2. Removing Modal Elements: Directly removes the modal and its backdrop from the DOM, ensuring that it doesn’t block interactions with the rest of the page.



Handle Alert and Confirm Dialog

Alerts and confirm dialogs can pause automation scripts unexpectedly, In this example, we will handle alert and confirm dialogs using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // Listen for dialog events (alerts, confirms, prompts)
  page.on("dialog", async (dialog) => {
    console.log(`Dialog message: ${dialog.message()}`);
    await dialog.accept(); // Automatically accept the dialog
  });

  // Navigate to a product page and add an item to the cart
  await page.goto("https://web-scraping.dev/product/1");
  await page.click(".add-to-cart");

  // Navigate to the cart page
  await page.goto("https://web-scraping.dev/cart");
  await page.waitForSelector(".cart-full .cart-item");

  // Click the 'Clear' button, which triggers a confirm dialog
  await page.click("(//button[contains(text(),'Clear')])[2]");

  // Verify that the cart is empty
  const cartItem = await page.$(".cart-item .cart-title");
  console.log(`Items in cart: ${cartItem}`); // Should be null if cleared

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

def handle_dialog(dialog):
    print(f"Dialog message: {dialog.message}")
    dialog.accept()  # Automatically accept the dialog

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()

    # Listen for dialog events (alerts, confirms, prompts)
    page.on('dialog', handle_dialog)

    # Navigate to a product page and add an item to the cart
    page.goto("https://web-scraping.dev/product/1")
    page.click(".add-to-cart")

    # Navigate to the cart page
    page.goto("https://web-scraping.dev/cart")
    page.wait_for_selector(".cart-full .cart-item")

    # Click the 'Clear' button, which triggers a confirm dialog
    page.click("(//button[contains(text(),'Clear')])[2]")

    # Verify that the cart is empty
    cart_item = page.query_selector(".cart-item .cart-title")
    print(f"Items in cart: {cart_item}")  # Should be None if cleared

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

When interacting with web pages, certain actions like clearing a cart may trigger alert or confirm dialogs. Playwright allows you to listen for these dialog events and handle them accordingly.

In this example, the script listens for any dialog, prints its message, and automatically accepts it. This ensures that the automation flow isn’t interrupted by unexpected dialogs.



Scroll to Element

When interacting with elements that aren’t visible, scrolling becomes necessary, In this example, we will scroll to a specific element on a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the scrollable page
  await page.goto("https://web-scraping.dev/scroll");

  // Select the target element (e.g., h1)
  const element = await page.$("h1");
  // Scroll the element into view if it's not already visible
  await element.scrollIntoViewIfNeeded();

  console.log("Scrolled to the h1 element");
  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the scrollable page
    page.goto('https://web-scraping.dev/scroll')

    # Select the target element (e.g., h1)
    element = page.query_selector('h1')
    # Scroll the element into view if it's not already visible
    element.scroll_into_view_if_needed()

    print("Scrolled to the h1 element")
    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

Scrolling to specific elements ensures that they are within the viewport, which is particularly useful when interacting with elements that are not immediately visible. The scrollIntoViewIfNeeded() function scrolls the page until the target element is visible.



Handle Infinite Scroll

Many modern websites use infinite scrolling to load additional content dynamically, In this example, we will handle infinite scroll on a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the testimonials page with infinite scroll
  await page.goto("https://web-scraping.dev/testimonials");

  // Wait for the main heading to ensure the page has loaded
  await page.waitForSelector("h1");
  let previousHeight = 0;

  while (true) {
    // Scroll to the bottom of the page
    await page.evaluate("window.scrollTo(0, document.body.scrollHeight)");
    // Wait for new content to load
    await page.waitForTimeout(1000);

    // Get the current scroll height
    const currentHeight = await page.evaluate("document.body.scrollHeight");
    // If the scroll height hasn't changed, exit the loop
    if (currentHeight === previousHeight) {
      break;
    }
    previousHeight = currentHeight;
  }

  console.log("Infinite scroll completed");
  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the testimonials page with infinite scroll
    page.goto('https://web-scraping.dev/testimonials')

    # Wait for the main heading to ensure the page has loaded
    page.wait_for_selector('h1')
    previous_height = 0

    while True:
        # Scroll to the bottom of the page
        page.evaluate('window.scrollTo(0, document.body.scrollHeight)')
        # Wait for new content to load
        page.wait_for_timeout(1000)

        # Get the current scroll height
        current_height = page.evaluate('document.body.scrollHeight')
        # If the scroll height hasn't changed, exit the loop
        if current_height == previous_height:
            break
        previous_height = current_height

    print('Infinite scroll completed')
    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

Infinite scrolling loads new content as the user scrolls down the page. This script automates scrolling to the bottom of the page repeatedly until no new content is loaded (i.e., the scroll height remains unchanged). This ensures that all dynamic content is loaded and can be interacted with or scraped.



Handle iFrames

Interacting with content within iFrames requires specific handling, In this example, we will handle iFrames on a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the page containing an iframe
  await page.goto("https://web-scraping.dev/iframes");

  // Select the iframe by its name attribute
  const frame = page.frame({ name: "iframe" });

  // Retrieve the text content of the h1 element within the iframe
  const title = await frame.textContent("h1");
  console.log(title);

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the page containing an iframe
    page.goto('https://web-scraping.dev/iframes')

    # Select the iframe by its name attribute
    frame = page.frame(name='iframe')

    # Retrieve the text content of the h1 element within the iframe
    title = frame.text_content('h1')
    print(title)

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

iFrames embed another HTML document within the current page. To interact with elements inside an iFrame, you first need to select the specific frame using its name or other identifying attributes. Once selected, you can perform actions or retrieve information from within the iFrame as if it were part of the main page.



Send Keyboard Input

Simulating keyboard input is crucial for automating form submissions or triggering specific actions, In this example, we will send keyboard input to an input field on a website using Playwright.



JavaScript

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

(async () => {
  // Launch the browser
  const browser = await chromium.launch();
  const page = await browser.newPage();
  // Navigate to the page with the input field
  await page.goto("https://web-scraping.dev/keyboard");

  // Fill the input field with text
  await page.fill("input", "Hello, World!");

  // Simulate pressing the Enter key
  await page.keyboard.press("Enter");

  // Wait for the h1 element to load as a result of the input
  await page.waitForSelector("h1");

  // Retrieve and print the text content of the h1 element
  const title = await page.textContent("h1");
  console.log(title);

  // Close the browser
  await browser.close();
})();
Enter fullscreen mode

Exit fullscreen mode



Python

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launch the browser
    browser = p.chromium.launch()
    page = p.new_page()
    # Navigate to the page with the input field
    page.goto('https://web-scraping.dev/keyboard')

    # Fill the input field with text
    page.fill('input', 'Hello, World!')

    # Simulate pressing the Enter key
    page.keyboard.press('Enter')

    # Wait for the h1 element to load as a result of the input
    page.wait_for_selector('h1')
    # Retrieve and print the text content of the h1 element
    title = page.text_content('h1')
    print(title)

    # Close the browser
    browser.close()
Enter fullscreen mode

Exit fullscreen mode

This script automates the process of entering text into an input field and simulating a keyboard event (pressing Enter). After the input and key press, it waits for a resulting element (h1) to load and then prints its content. This is useful for testing form submissions, search functionalities, and other interactive features on web pages.



FAQ

To wrap this introduction up let’s take a look at some frequently asked questions regarding Playwright Examples.



Which browsers does Playwright support?

Playwright supports three major browser engines: Chromium (used by Google Chrome and Edge), WebKit (used by Safari), and Firefox.



Does Playwright support proxy configurations?

Yes, you can configure Playwright to use a proxy server by specifying proxy details when launching the browser.



Can Playwright take screenshots or generate PDFs?

Yes, Playwright can capture screenshots of webpages or generate PDF files, which are useful for debugging, reporting, and documentation.



Summary

In this article, we explored javascript playwright examples as well as python playwright examples for common web automation tasks.

We covered a wide range of examples and practical implementations, demonstrating how to:

  • Automate form submissions
  • Capture local storage and cookies
  • Reuse login sessions by saving and re-injecting cookies
  • Wait for elements to load
  • Take screenshots and convert pages to PDF
  • Handle file downloads and file uploads
  • Close modal/cookie popups
  • Manage alerts/confirm dialogs
  • Scroll to specific elements and handle infinite scroll
  • Work with iFrames
  • Send keyboard input

With practical examples and insights, it equips both beginners and advanced users with tools for efficient and scalable browser automation.



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.