Select Element by XPath
While Playwright recommends CSS selectors and user-facing locators, you can also use XPath. Prefix your selector with xpath= or use // directly.
For input fields, read the current value with locator.inputValue() — not textContent(), because inputs do not have inner text.
Example: const value = await page.locator('//input[@name="email"]').inputValue(); reads the input whose name attribute is email.
On the Text Fields page, the pre-filled text field has:
- ID:
preFilledTextField - Value:
Codekru
Task:
- Navigate to
https://testkru.com/Elements/TextFields - Find the pre-filled field with an XPath that matches an
inputby itsid - Print its value using
inputValue() - Find the disabled field (ID
disabledField) the same way - Print its value using
inputValue()
Expected output:
Pre-filled value: Codekru Disabled value: Codekru
TestKru