Playwright with JavaScript: locator() vs getByTestId()
Description: Your selector broke again? Playwright with JavaScript: locator() vs getByTestId(). Which one should you use? Explained with the real HTML for every example.
❌ locator(): CSS or XPath selectors
HTML: a button element with class="primary" and the text Save
page.locator('button.primary') // CSS
page.locator('//button') // XPath
Playwright auto-detects CSS or XPath.
⚠️ The problem:
A redesign renames the class (primary → btn-save) and the selector finds 0 matches.
Playwright docs: CSS and XPath can be tied to the DOM structure.
✅ getByTestId(): a dedicated test attribute
HTML: a button element with data-testid="directions"
await page.getByTestId('directions').click();
→ change the class all you want, it still works
→ looks for data-testid by default
⚙️ Different attribute in your app? Set it once in playwright.config.js:
use: { testIdAttribute: 'data-pw' }
→ your tests stay the same
🏆 Pick in this order:
1. getByRole() / getByText()
2. getByTestId()
3. locator() with CSS / XPath
Stable test IDs beat brittle CSS.
📘 Playwright locators docs: https://playwright.dev/docs/locators
💬 Does your Playwright JavaScript framework use test IDs or CSS selectors? 👇
🔔 Follow Testing Funda for daily Playwright JavaScript & automation testing Shorts.
#playwright #javascript #automationtesting
🔔 Subscribe for FREE Software Testing Tutorials → https://www.youtube.com/c/TestingFunda
==================================
🚀 WHAT YOU'LL LEARN ON THIS CHANNEL
==================================
✅ Manual & Automation Testing
✅ Selenium, Cypress, Playwright & Appium
✅ API Testing with Postman
✅ Performance Testing with JMeter
✅ Java & Python for Testers
✅ Agile, SDLC, STLC & Interview Prep
==================================
📚 FREE TUTORIAL PLAYLISTS
==================================
🔹 Selenium Tutorials (Free)
https://www.youtube.com/watch?v=5hyEnZT9AkQ&list=PLBw1ubD1J1UjP1rZMbU8NSh2ajE12Yjjf
🔹 JMeter Performance Testing (Free)
https://www.youtube.com/watch?v=s33Bqz0vAUw&list=PLBw1ubD1J1UgVniCSUlg_LKRzlF11CZZB
🔹 Postman API Testing (Free)
https://www.youtube.com/playlist?list=PLBw1ubD1J1UgVniCSUlg_LKRzlF11CZZB
🔹 Java Programming for Testers (Free)
https://www.youtube.com/watch?v=C8HRfmJ_ZZQ&list=PLBw1ubD1J1UjGNxe10eY-H7e9cQ12Qk_l
🔹 Cypress Automation Testing (Free)
https://www.youtube.com/watch?v=OvCLwh5vN8g&list=PLBw1ubD1J1Ujws5XLs7BGJqQkQVpJtmBJ
🔹 Agile Testing Series (Free)
https://www.youtube.com/playlist?list=PLBw1ubD1J1Uif-cFY6BJRbjc3Ah9bKpSd
🔹 Animated Software Testing Videos (Free)
https://www.youtube.com/watch?v=jRcsuCrQI_s&list=PLBw1ubD1J1UhnuUX3Kd5OP5r9qaDHie-T
==================================
🔗 CONNECT WITH US
==================================
📺 YouTube → https://www.youtube.com/c/TestingFunda
==================================
⚠️ DISCLAIMER
==================================
All content is for educational purposes only.
#SoftwareTesting #AutomationTesting #Selenium #QA #ManualTesting #Postman #JMeter #Cypress #JavaTesting #APITesting #PerformanceTesting #QAEngineer #SDET #TestingTutorials #FreeTutorials #2026 Close description
❌ locator(): CSS or XPath selectors
HTML: a button element with class="primary" and the text Save
page.locator('button.primary') // CSS
page.locator('//button') // XPath
Playwright auto-detects CSS or XPath.
⚠️ The problem:
A redesign renames the class (primary → btn-save) and the selector finds 0 matches.
Playwright docs: CSS and XPath can be tied to the DOM structure.
✅ getByTestId(): a dedicated test attribute
HTML: a button element with data-testid="directions"
await page.getByTestId('directions').click();
→ change the class all you want, it still works
→ looks for data-testid by default
⚙️ Different attribute in your app? Set it once in playwright.config.js:
use: { testIdAttribute: 'data-pw' }
→ your tests stay the same
🏆 Pick in this order:
1. getByRole() / getByText()
2. getByTestId()
3. locator() with CSS / XPath
Stable test IDs beat brittle CSS.
📘 Playwright locators docs: https://playwright.dev/docs/locators
💬 Does your Playwright JavaScript framework use test IDs or CSS selectors? 👇
🔔 Follow Testing Funda for daily Playwright JavaScript & automation testing Shorts.
#playwright #javascript #automationtesting
🔔 Subscribe for FREE Software Testing Tutorials → https://www.youtube.com/c/TestingFunda
==================================
🚀 WHAT YOU'LL LEARN ON THIS CHANNEL
==================================
✅ Manual & Automation Testing
✅ Selenium, Cypress, Playwright & Appium
✅ API Testing with Postman
✅ Performance Testing with JMeter
✅ Java & Python for Testers
✅ Agile, SDLC, STLC & Interview Prep
==================================
📚 FREE TUTORIAL PLAYLISTS
==================================
🔹 Selenium Tutorials (Free)
https://www.youtube.com/watch?v=5hyEnZT9AkQ&list=PLBw1ubD1J1UjP1rZMbU8NSh2ajE12Yjjf
🔹 JMeter Performance Testing (Free)
https://www.youtube.com/watch?v=s33Bqz0vAUw&list=PLBw1ubD1J1UgVniCSUlg_LKRzlF11CZZB
🔹 Postman API Testing (Free)
https://www.youtube.com/playlist?list=PLBw1ubD1J1UgVniCSUlg_LKRzlF11CZZB
🔹 Java Programming for Testers (Free)
https://www.youtube.com/watch?v=C8HRfmJ_ZZQ&list=PLBw1ubD1J1UjGNxe10eY-H7e9cQ12Qk_l
🔹 Cypress Automation Testing (Free)
https://www.youtube.com/watch?v=OvCLwh5vN8g&list=PLBw1ubD1J1Ujws5XLs7BGJqQkQVpJtmBJ
🔹 Agile Testing Series (Free)
https://www.youtube.com/playlist?list=PLBw1ubD1J1Uif-cFY6BJRbjc3Ah9bKpSd
🔹 Animated Software Testing Videos (Free)
https://www.youtube.com/watch?v=jRcsuCrQI_s&list=PLBw1ubD1J1UhnuUX3Kd5OP5r9qaDHie-T
==================================
🔗 CONNECT WITH US
==================================
📺 YouTube → https://www.youtube.com/c/TestingFunda
==================================
⚠️ DISCLAIMER
==================================
All content is for educational purposes only.
#SoftwareTesting #AutomationTesting #Selenium #QA #ManualTesting #Postman #JMeter #Cypress #JavaTesting #APITesting #PerformanceTesting #QAEngineer #SDET #TestingTutorials #FreeTutorials #2026 Close description
