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.

1
2
3
4
5
6
7
8
9
10
11
12
13

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.

1
2
3
4
5
6
7
8
9
10
11
12
13

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

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

Roberta Aukstikalnyte

2025-07-31

Playwright Web Scraping Tutorial for 2025
Playwright Web Scraping Tutorial for 2025
Iveta Vistorskyte avatar

Iveta Vistorskyte

2025-01-02

Advanced Web Scraping With Python Tactics in 2025
vytenis kaubre avatar

Vytenis Kaubrė

2025-01-01

Get the latest news from data gathering world

I'm interested