How to find elements by XPath selectors in Playwright?

Playwright supports one of the most popular ways to parse HTML content in web scraping - XPath selectors. To use XPath in Playwright we can use page.locator() method and prefix our selector with xpath= or //. For example:

from playwright.sync_api import sync_playwright

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()
    page.goto("https://google.com/")

    h2_element = page.locator("//h2")
    # or 
    h2_element = page.locator("xpath=//h2")

⚠ It's possible that this command will try to find elements before the page has fully loaded if it's a dynamic javascript page. For more see How to wait for page to load in Playwright?

Also see: How to find elements by CSS selectors in Playwright?

Related Posts

How to Scrape Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.

Scraping Dynamic Websites Using Web Browsers

Introduction to using web automation tools such as Puppeteer, Playwright, Selenium and ScrapFly to render dynamic websites for web scraping