<?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: Anchor Locator</title>
    <description>The latest articles on DEV Community by Anchor Locator (@anchorlocator).</description>
    <link>https://dev.to/anchorlocator</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%2F4208560%2F7f93d72f-da42-4802-88af-f5d280f81075.png</url>
      <title>DEV Community: Anchor Locator</title>
      <link>https://dev.to/anchorlocator</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anchorlocator"/>
    <language>en</language>
    <item>
      <title>Sort-safe table locators in XPath: anchor on the row, pick the column by its header</title>
      <dc:creator>Anchor Locator</dc:creator>
      <pubDate>Thu, 05 Nov 2026 06:00:38 +0000</pubDate>
      <link>https://dev.to/anchorlocator/sort-safe-table-locators-in-xpath-anchor-on-the-row-pick-the-column-by-its-header-2907</link>
      <guid>https://dev.to/anchorlocator/sort-safe-table-locators-in-xpath-anchor-on-the-row-pick-the-column-by-its-header-2907</guid>
      <description>&lt;p&gt;A table test that fails after a sort is annoying. A table test that keeps passing after a sort, while checking a different row, is worse. Nothing goes red, and the bug ships.&lt;/p&gt;

&lt;p&gt;This tutorial builds a sort-safe table locator in XPath, step by step: anchor on the row you mean, then pick the cell by its column header. Examples use Playwright, and the XPath works the same in Selenium.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/RhG_8nKYZLc" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: row and column by position
&lt;/h2&gt;

&lt;p&gt;Here's a small users table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;&lt;/span&gt;Name&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;&lt;/span&gt;Email&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;&lt;/span&gt;Status&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;&lt;/span&gt;Plan&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Ana Cruz&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;ana@example.com&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Active&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Team&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Ben Ortiz&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;ben@example.com&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Invited&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Starter&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Chloe Lim&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;chloe@example.com&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Active&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;/span&gt;Team&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want to check that Ben's status is "Invited". The quick locator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;xpath=//tbody/tr[2]/td[3]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toHaveText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invited&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tr[2]/td[3]&lt;/code&gt; means "second row, third cell". It isn't tied to Ben at all. Sort the table by Status and Chloe moves into row 2. The locator still finds a cell, it now reads "Active", and depending on what you assert, the test either fails for the wrong reason or passes against the wrong person.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: anchor on the row's own text
&lt;/h2&gt;

&lt;p&gt;Pick a cell value that identifies the row and won't change when the table is sorted. An email or an ID works better than a display name, because it's more likely to be unique.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;normalize-space&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'ben@example.com'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;ancestor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it left to right: find the cell whose text is &lt;code&gt;ben@example.com&lt;/code&gt;, then walk up with &lt;code&gt;ancestor::tr[1]&lt;/code&gt; to the nearest row that contains it. Wherever sorting puts Ben, this returns Ben's row.&lt;/p&gt;

&lt;p&gt;Now take the third cell of that row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;normalize-space&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'ben@example.com'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;ancestor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That survives sorting. It doesn't survive someone moving the Status column, because &lt;code&gt;td[3]&lt;/code&gt; is still a position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: pick the column by its header
&lt;/h2&gt;

&lt;p&gt;The column header is a stable anchor too. Count how many header cells come before "Status", add 1, and you have the column index, worked out at run time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ancestor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;table&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;normalize-space&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Status'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;preceding-sibling&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put it inside the row's &lt;code&gt;td[...]&lt;/code&gt; predicate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@playwright/test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ben is still invited after sorting&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.com/admin/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;statusFor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`xpath=//td[normalize-space(.)='&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;']/ancestor::tr[1]`&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="s2"&gt;`/td[count(ancestor::table[1]//th[normalize-space(.)='Status']/preceding-sibling::th)+1]`&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;statusFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ben@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toHaveText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invited&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;columnheader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Status&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// sort&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;statusFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ben@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toHaveText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invited&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it against three versions of the table above: the original order, sorted by Status, and with the Status column moved to second place. It returns Ben's "Invited" cell every time. The &lt;code&gt;tr[2]/td[3]&lt;/code&gt; version returns "Invited", then "Active", then an email address.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ancestor::table[1]&lt;/code&gt; keeps the header lookup inside the same table as the row, so a second table on the page with its own "Status" column can't throw the count off.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things that can still trip it up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Duplicate row text.&lt;/strong&gt; If two rows share the anchor value, the locator matches both and Playwright's strict mode will complain. That's a good failure. Pick a more unique column, such as an email or an ID.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;colspan and rowspan.&lt;/strong&gt; Counting preceding &lt;code&gt;th&lt;/code&gt; elements assumes one header cell per column. Merged headers shift the count, so check grouped headers by hand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Header text with extra markup.&lt;/strong&gt; A sort icon or a hidden "sorted ascending" label inside the &lt;code&gt;th&lt;/code&gt; changes its text. &lt;code&gt;normalize-space(.)&lt;/code&gt; handles stray whitespace, but not extra words; use &lt;code&gt;contains()&lt;/code&gt; on the visible label if you need to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Virtualized tables.&lt;/strong&gt; If the grid only renders visible rows, scroll the row into view before you assert.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why this counts as a resilient locator
&lt;/h2&gt;

&lt;p&gt;Neither part of the final XPath depends on a position you don't control. The row is found by its data, and the column is found by its header. Sorting, filtering and reordering columns all leave both anchors in place, so the locator keeps pointing at the same cell, and an assertion like &lt;code&gt;toHaveText&lt;/code&gt; tells you when the data itself changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building it without hand-writing XPath
&lt;/h2&gt;

&lt;p&gt;You don't have to type the &lt;code&gt;count(...)&lt;/code&gt; part each time. Sort-safe table locators are a free feature in Anchor Locator, and the free Select Anchor button lets you pick any nearby element, like a row's email cell or a column header, as the anchor for the axes XPath. If you record tests, you can change any recorded step's anchor when the one it picked isn't the stable one.&lt;/p&gt;

&lt;p&gt;I'm the solo developer of Anchor Locator.&lt;/p&gt;

&lt;p&gt;Your page data never leaves your browser; only license checks go online.&lt;/p&gt;

&lt;p&gt;Free on the Chrome Web Store: &lt;a href="https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg?utm_source=devto" rel="noopener noreferrer"&gt;https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg?utm_source=devto&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>playwright</category>
      <category>selenium</category>
      <category>automation</category>
    </item>
    <item>
      <title>following-sibling vs following in XPath: make your locator hit the right element</title>
      <dc:creator>Anchor Locator</dc:creator>
      <pubDate>Thu, 29 Oct 2026 06:12:29 +0000</pubDate>
      <link>https://dev.to/anchorlocator/following-sibling-vs-following-in-xpath-make-your-locator-hit-the-right-element-400c</link>
      <guid>https://dev.to/anchorlocator/following-sibling-vs-following-in-xpath-make-your-locator-hit-the-right-element-400c</guid>
      <description>&lt;p&gt;A locator that fails is annoying. A locator that passes while clicking the wrong element is worse, because the test stays green and the bug slips through.&lt;/p&gt;

&lt;p&gt;Two XPath axes help with a lot of these cases: &lt;code&gt;following-sibling::&lt;/code&gt; and &lt;code&gt;following::&lt;/code&gt;. They look alike, but they search very different parts of the page. Here's when to use each, with Playwright examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: index-based locators
&lt;/h2&gt;

&lt;p&gt;Say a pricing page has two cards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Starter&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"price"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;$0&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Choose&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Team plan&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"price"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;$12&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Choose&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick locator for the Team plan button might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xpath=(//button[normalize-space(.)='Choose'])[2]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works until someone reorders the cards or adds a new plan in front. Then &lt;code&gt;[2]&lt;/code&gt; points at a different card, the click still succeeds, and your test quietly buys the wrong plan.&lt;/p&gt;

&lt;p&gt;The fix is to anchor on something that describes the element you want: the card's title.&lt;/p&gt;

&lt;h2&gt;
  
  
  following-sibling: same parent, after the anchor
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;following-sibling::&lt;/code&gt; looks only at elements that share the anchor's parent and come after it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@playwright/test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;choose the Team plan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.com/pricing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chooseTeam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xpath=//h3[normalize-space(.)='Team plan']/following-sibling::button[1]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chooseTeam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it left to right: find the &lt;code&gt;h3&lt;/code&gt; whose text is "Team plan", then take the first &lt;code&gt;button&lt;/code&gt; after it inside the same card. Reordering the cards doesn't matter, because the locator is tied to the title, not a position on the page.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;following-sibling::&lt;/code&gt; is the safer choice whenever the anchor and target sit side by side under one parent. It can't wander out of the card.&lt;/p&gt;

&lt;h2&gt;
  
  
  When following-sibling finds nothing
&lt;/h2&gt;

&lt;p&gt;Forms often wrap the label and the input in separate containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"row"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&amp;lt;label&amp;gt;&lt;/span&gt;Email&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;normalize-space&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;following-sibling&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The label has no siblings at all; the &lt;code&gt;input&lt;/code&gt; lives in a different &lt;code&gt;div&lt;/code&gt;. This is where &lt;code&gt;following::&lt;/code&gt; comes in.&lt;/p&gt;

&lt;h2&gt;
  
  
  following: anything after the anchor in the document
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;following::&lt;/code&gt; looks at every element that comes after the anchor in document order, at any depth (excluding the anchor's own descendants).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fill the email field&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.com/signup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xpath=//label[normalize-space(.)='Email']/following::input[1]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;qa@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;qa@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;following::input[1]&lt;/code&gt; means "the first input after the Email label". Extra wrapper divs, a new layout grid or a renamed class don't break it, because nothing in the path depends on the structure between the label and the field.&lt;/p&gt;

&lt;h2&gt;
  
  
  The catch with following::
&lt;/h2&gt;

&lt;p&gt;Because &lt;code&gt;following::&lt;/code&gt; searches the rest of the document, it can reach too far. If the Email field is removed or renamed, &lt;code&gt;following::input[1]&lt;/code&gt; might grab the next input on the page, such as Password. The locator still matches, and you're back to a test that passes while doing the wrong thing.&lt;/p&gt;

&lt;p&gt;A few habits keep it honest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefer &lt;code&gt;following-sibling::&lt;/code&gt; when the structure allows it.&lt;/strong&gt; It stays inside one parent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the anchor close to the target.&lt;/strong&gt; A label or heading right before the element beats one at the top of the page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filter the target, not only the anchor.&lt;/strong&gt; &lt;code&gt;following::input[@type='email'][1]&lt;/code&gt; is stricter than &lt;code&gt;following::input[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assert what you acted on.&lt;/strong&gt; A quick &lt;code&gt;toHaveValue&lt;/code&gt; or &lt;code&gt;toHaveAttribute&lt;/code&gt; check catches a wrong match early.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick rule of thumb
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Axis&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Anchor and target share a parent (cards, list items, table cells in a row)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;following-sibling::&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anchor and target are in different wrappers (labels and inputs in grid layouts)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;following::&lt;/code&gt; with &lt;code&gt;[1]&lt;/code&gt; and a target filter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Target comes before the anchor&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;preceding-sibling::&lt;/code&gt; or &lt;code&gt;preceding::&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Picking the anchor without hand-writing XPath
&lt;/h2&gt;

&lt;p&gt;Writing these by hand is fine once you know the axes, but finding a good anchor on a busy page takes time. In Anchor Locator, the free &lt;strong&gt;Select Anchor&lt;/strong&gt; button lets you choose any nearby element as the anchor, such as a heading, a label or a column header, and it builds the axes XPath from that anchor to your target. In the Recorder, you can change any recorded step's anchor when the one it picked isn't the stable one.&lt;/p&gt;

&lt;p&gt;I'm the solo developer of Anchor Locator.&lt;/p&gt;

&lt;p&gt;Your page data never leaves your browser; only license checks go online.&lt;/p&gt;

&lt;p&gt;Free on the Chrome Web Store: &lt;a href="https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg" rel="noopener noreferrer"&gt;https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>playwright</category>
      <category>selenium</category>
      <category>automation</category>
    </item>
    <item>
      <title>Your XPath works today and breaks tomorrow: anchor it instead</title>
      <dc:creator>Anchor Locator</dc:creator>
      <pubDate>Mon, 26 Oct 2026 11:23:25 +0000</pubDate>
      <link>https://dev.to/anchorlocator/your-xpath-works-today-and-breaks-tomorrow-anchor-it-instead-4ie7</link>
      <guid>https://dev.to/anchorlocator/your-xpath-works-today-and-breaks-tomorrow-anchor-it-instead-4ie7</guid>
      <description>&lt;p&gt;An XPath that works today can break after a small DOM change: a wrapper div gets added, a class gets renamed, or an index shifts. Long absolute paths and positional indexes are the usual cause.&lt;/p&gt;

&lt;p&gt;A sturdier pattern is to anchor the locator to something that rarely changes, such as a visible label or heading, and use an XPath axis to reach the element you need from there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="ow"&gt;div&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="ow"&gt;div&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="ow"&gt;div&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Anchored:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xpath"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;normalize-space&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;following&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Playwright:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xpath=//label[normalize-space(.)='Email']/following::input[1]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;qa@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three axes cover most cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;following::&lt;/code&gt; reaches the first matching element after the anchor, like an input after its label.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;following-sibling::&lt;/code&gt; reaches a neighbor at the same level, like a button next to a heading.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ancestor::&lt;/code&gt; climbs up to a container, like a card or row, and then lets you search inside it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trick is choosing a good anchor: visible text or a stable attribute that a designer is unlikely to change.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: I'm the solo developer of Anchor Locator, a Chrome extension that builds axes-based XPath like this. It picks an anchor automatically and lets you choose any element with the Select Anchor button. Generating locators is free. The Recorder exports Playwright, Selenium and Cypress, and its first 10 steps are free. Page data never leaves your browser; only license checks go online.&lt;/em&gt;&lt;br&gt;
Chrome Web Store: &lt;a href="https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg" rel="noopener noreferrer"&gt;https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>playwright</category>
      <category>selenium</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
