Best practices

  • Choose CSS selectors that are both specific and stable to avoid selecting the wrong elements. Target IDs, data attributes, or uniquely structured paths whenever possible.

  • Prefer text selectors for buttons and links to make tests more readable and maintainable, as they mimic user interactions.

  • Utilize XPath for navigating complex DOM structures or when needing to perform more advanced element searches.

  • Combine CSS, text, or role-based selectors to fine-tune element targeting – especially helpful on dynamic pages where content or structure can shift frequently.

Scrollable code block. Use arrow keys to scroll.

Common issues

  • Ensure that Playwright locators are updated if the structure of the web page changes, as stale selectors can cause tests to fail.

  • Always verify that a locator matches only one element unless your test logic expects multiple. Selecting multiple unintended elements can cause test failures or flaky behavior.

  • Use Playwright's auto-wait feature by default to handle elements that might take time to appear or become interactable, avoiding flaky tests.

  • The Playwright Inspector is an invaluable tool for troubleshooting. Use it to explore the page structure, validate selectors, and try out locators interactively in the browser.

Scrollable code block. Use arrow keys to scroll.

Try Oyxlabs' Proxies & Scraper API

Residential Proxies

Self-Service

Human-like scraping without IP blocking

From

8

Datacenter Proxies

Self-Service

Fast and reliable proxies for cost-efficient scraping

From

1.2

Web scraper API

Self-Service

Public data delivery from a majority of websites

From

49

Useful resources

Playwright Web Scraping Tutorial for 2026
Playwright Web Scraping Tutorial for 2026
Iveta Liupševičė

Iveta Liupševičė

2026-02-11

How to Bypass CAPTCHA With Playwright
How to Bypass CAPTCHA With Playwright 2026
author avatar

Yelyzaveta Hayrapetyan

2025-10-14

24 Best Website Testing Tools To Use In 2026
24 Best Website Testing Tools To Use In 2026
vytenis kaubre avatar

Vytenis Kaubrė

2025-01-02

Get the latest news from data gathering world