Have a question?
Message sent Close
View Categories

Locators in Playwright CSS Selector vs XPath

Table of Contents

๐Ÿ“„
CSSSelector.spec.ts
test('Locator with CSS Selector', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site/');

  // First name (using CSS ID selector)
  await page.locator('css=#vfb-5').fill('Vinoth');

  // Last name (short form without "css=")
  await page.locator('#vfb-7').fill('R');
});

๐Ÿ“„
XPath.spec.ts
test('Locator with XPath Selector', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site/');

  // Email Id (explicit XPath)
  await page.locator("xpath=//input[@id='vfb-14']").fill('vinothrwins@gmail.com');

  // Mobile Number (short form without "xpath=")
  await page.locator("//input[@id='vfb-19']").fill('123456789');
});