Skip to content
CodeKruPro

Select Element by XPath

8/70

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:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the pre-filled field with an XPath that matches an input by its id
  3. Print its value using inputValue()
  4. Find the disabled field (ID disabledField) the same way
  5. Print its value using inputValue()

Expected output:

Pre-filled value: Codekru
Disabled value: Codekru