Locator Syntax Rules | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Locators and Assertions
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to utilize HTML fundamentals in Playwright to interact with web elements. We focus on the Locator Syntax Rules for finding elements in HTML. Key Concepts Finding Elements by Tag Name: Use page.locator('tagName') to find elements by their tag. Finding Elements by ID: Use page.locator('#idValue') where # indicates an ID selector. Finding Elements by Class: Use page.locator('.classValue') where . indicates a class selector. Finding Elements by Attribute: Use page.locator('[attributeName="value"]') to select elements based on any attribute. Combining Selectors You can combine multiple selectors without spaces, e.g., page.locator('input[placeholder="email"].shapeRectangle') to target elements that match all specified criteria. Using XPath Although XPath is supported, it is not recommended. Instead, prioritize user-visible locators for better reliability. Finding by Text Partial Match: Use page.locator('text=partialText') . Exact Match: Use page.locator('text="exactText"') , ensuring the text matches the HTML format exactly. In summary, the Locator Method in Playwright allows for flexible and powerful element selection using various criteria, enhancing your ability to automate web interactions effectively.