Skip to content
CodeKruPro

Locator Chaining and Filtering

12/70

Locator Chaining and Filtering

Playwright locators can be chained to narrow down results:

  • locator.locator(selector) — find within an already-located element
  • locator.filter({ hasText: 'text' }) — filter by text content

On the Links page, links are organized in sections.

Task:

  1. Navigate to https://testkru.com/Elements/Links
  2. Start from a page.locator() that matches every a element
  3. Narrow it to the link whose text contains "Google" with .filter() and the hasText option
  4. Print its text with textContent() and its href with getAttribute()

Expected output:

Text: Visit Google Website
Href: https://www.google.com