Best practices

  • Use specific attribute selectors when possible to improve the accuracy and robustness of your element targeting, such as `data-test-id` or `role`.

  • Combine CSS selectors and text selectors to refine element targeting and handle dynamic content more effectively.

  • Use page.locator() instead of older methods like page.querySelector() to benefit from Playwright’s built-in auto-waiting for visibility and readiness before interactions.

  • When using XPath, ensure it is as concise as possible and avoid absolute paths to maintain selector resilience against changes in the DOM structure.

Scrollable code block. Use arrow keys to scroll.

Common issues

  • Playwright’s role selectors are ideal for targeting accessible elements, improving test readability and cross-browser reliability.

  • Experiment with chaining selectors in Playwright to navigate complex DOM structures more efficiently, such as `page.locator('.container >> .item')`.

  • To avoid interacting with hidden elements, use the :visible pseudo-class in your selectors. This ensures your script only interacts with elements actually shown to users.

  • When dealing with nested content, use frameLocator() to easily access and interact with elements inside iframes without additional workarounds.

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

books illustration Scrapy Playwright
Scrapy Playwright Tutorial: How to Scrape JavaScript Websites
Agne Matuseviciute avatar

Agnė Matusevičiūtė

2025-07-31

Advanced Web Scraping With Python Tactics in 2026
vytenis kaubre avatar

Vytenis Kaubrė

2025-01-01

Get the latest news from data gathering world