<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Babatunde Hammed</title>
    <description>The latest articles on DEV Community by Babatunde Hammed (@2unde).</description>
    <link>https://dev.to/2unde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4183149%2Fd4bbcb4e-dcd4-40d0-972e-1026990e6b38.png</url>
      <title>DEV Community: Babatunde Hammed</title>
      <link>https://dev.to/2unde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/2unde"/>
    <language>en</language>
    <item>
      <title>Why Your Playwright Selectors Keep Breaking (And How to Fix Them)</title>
      <dc:creator>Babatunde Hammed</dc:creator>
      <pubDate>Wed, 14 Oct 2026 12:18:56 +0000</pubDate>
      <link>https://dev.to/2unde/why-your-playwright-selectors-keep-breaking-and-how-to-fix-them-4egp</link>
      <guid>https://dev.to/2unde/why-your-playwright-selectors-keep-breaking-and-how-to-fix-them-4egp</guid>
      <description>&lt;p&gt;You've seen this before. The test passes on your machine, fails in CI, and the error is something like "locator.click: Timeout 30000ms exceeded." You re-run it. It passes. You re-run it again. It fails. Congratulations — you've met a flaky test, and the culprit is almost never the assertion. It's the locator.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Three Failure
&lt;/h2&gt;

&lt;p&gt;Modes nth-child selectors- `&lt;code&gt;page.locator('div.container &amp;gt; div:nth-child(3) &amp;gt; button')&lt;/code&gt;&lt;br&gt;
works today because the button happens to be the third child. Tomorrow a designer adds a banner above it, and the selector silently points at the wrong element — or nothing at all. The test doesn't error; it clicks the wrong thing, and your suite lies to you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fixed sleeps&lt;/strong&gt;- &lt;code&gt;await page.waitForTimeout(3000)&lt;/code&gt; is the most common anti-pattern in Playwright codebases. It assumes the page loads in three seconds. On a fast machine it wastes time; on a slow CI runner it times out. Either way, you're not waiting for the right thing — you're waiting for a clock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS paths&lt;/strong&gt;. &lt;code&gt;.btn-primary-v2&lt;/code&gt; breaks the moment someone renames the class during a redesign. And if the class was generated by a CSS-in-JS library, it changes on every build.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;

&lt;p&gt;Prefer getByRole and getByText. These locators target what the user sees and what assistive tech reads — accessible names, roles, visible text. A button's role doesn't change when its styling does.&lt;/p&gt;

&lt;p&gt;// Fragile&lt;br&gt;
&lt;code&gt;await page.locator('#submit-btn-47').click();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// Resilient&lt;br&gt;
&lt;code&gt;await page.getByRole('button', { name: 'Submit' }).click();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace fixed sleeps with web-first assertions. Playwright's &lt;code&gt;expect&lt;/code&gt; auto-retries until the condition passes or the timeout hits, so you never guess at timing:&lt;/p&gt;

&lt;p&gt;// Fragile&lt;br&gt;
&lt;code&gt;await page.waitForTimeout(3000);&lt;/code&gt;&lt;br&gt;
&lt;code&gt;await expect(page.getByText('Order confirmed')).toBeVisible();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// Resilient&lt;br&gt;
&lt;code&gt;await expect(page.getByText('Order confirmed')).toBeVisible();&lt;/code&gt;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
The assertion itself is the wait. No sleep needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice
&lt;/h2&gt;

&lt;p&gt;Reading about this is one thing. Writing locators under pressure is another. I built LocatorLeague — a free playground with twenty-nine browser challenges where you write a Playwright locator, the platform runs it in a real Chromium instance, and you get pass/fail feedback plus a video of the run. No install, no setup. The Foundations track is free at &lt;a href="https://locatorleague.dev" rel="noopener noreferrer"&gt;https://locatorleague.dev&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;What's the flakiest test in your suite? Drop it in the comments — half the time the fix is one locator swap away.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>playwright</category>
    </item>
  </channel>
</rss>
