Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Created July 30, 2025 16:09
Show Gist options
  • Save nadvolod/107e8e6b376226f6ef1d89ab2697e8f1 to your computer and use it in GitHub Desktop.
Save nadvolod/107e8e6b376226f6ef1d89ab2697e8f1 to your computer and use it in GitHub Desktop.
Xpath selector is okay. Brittle selectors are the problem
test('XPath as syntax vs locator strategy', async ({ page }) => {
await page.goto('https://example.com/app');
// ✅ Test ID strategy - using XPath syntax
await page.locator('//button[@data-testid="submit-order"]').click();
// ✅ Test ID strategy - using CSS syntax
await page.locator('[data-testid="submit-order"]').click();
// ✅ Accessibility strategy - using XPath syntax
await page.locator('//button[@aria-label="Submit your order"]').click();
// ✅ Accessibility strategy - using CSS syntax
await page.locator('[aria-label="Submit your order"]').click();
// ✅ User-facing strategy - XPath excels here
await page.locator('//button[text()="Submit Order"]').click();
// ❌ BRITTLE strategy - regardless of syntax
await page.locator('//div[2]/form/div[3]/button[1]').click(); // XPath
await page.locator('div:nth-child(2) form div:nth-child(3) button:first-child').click(); // CSS
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment