How do you use XPath in Cypress?
Cypress has no built-in XPath support, and the @cypress/xpath plugin is marked unsupported on npm, so a new project does not take it. Built-in commands cover what XPath is used for: cy.contains for text, .closest() and .parents() upward, .siblings() and .next() sideways, .find() downward. If XPath seems unavoidable, markup usually lacks attributes, so add data-cy.
Cypress · Selectors
An old Selenium test has an XPath locator. How do you carry it over to Cypress?
Cypress has no built-in XPath support. There used to be a plugin, @cypress/xpath, but npm marks it as no longer supported (last release in 2022), so a new project does not take it. The good news: the jobs people reach for XPath to do are covered by built-in commands.
How it works
Cypress selectors are CSS plus retrying query commands. XPath does three things that need replacing: find an element by text, go up to an ancestor and move to a sibling. The counterparts: cy.contains (text), .closest()/.parents() (up), .siblings()/.next()/.prev() (sideways), .find() (down). Each is a query, so the whole chain retries until the element appears.
What replaces what
Example: from XPath to a chain
// XPath: //td[text()='Order #1042']/parent::tr//button
cy.contains('td', 'Order #1042')
.closest('tr')
.find('button')
.click();
The chain reads like a path in the table: find the cell, go up to the row, find the button. If you need semantics, Cypress 16.2 has a built-in cy.getByRole('button', { name: 'Delete' }) (earlier you took @testing-library/cypress for it); the most stable choice is a dedicated data-cy attribute.
Limits and alternatives
What the built-ins do not give: complex axes such as following:: or position-based picks in an arbitrary predicate. Such cases almost always point to weak markup; add a data-cy instead. Playwright supports XPath natively (page.locator('xpath=...'); a string starting with // is recognised automatically), but recommends getByRole, getByText and getByTestId.
Harder cases
Two common XPath jobs that first look hopeless. The first: "the cell after a header". In Cypress it is cy.contains('th', 'Status').next(), and the column number comes from the jQuery method through .invoke('index'). The second: "an element that contains a child with some text". Here cy.contains('li', 'Cart') works (it finds the node containing the text) or .filter(':has(...)') when you need a selector with a condition.
// XPath: //tr[td[text()='Ivan']]/td[3]
cy.contains('td', 'Ivan')
.closest('tr')
.find('td')
.eq(2)
.should('have.text', 'Active');
// Built-in getByRole (Cypress 16.2+)
cy.getByRole('row', { name: /Ivan/ })
.getByRole('button', { name: 'Delete' })
.click();
The index .eq(2) is brittle: column order changes. It is more reliable when the cell has its own data-cy, for instance data-cy="status", and the chain ends with .find('[data-cy=status]'). A good topic for developers: if you cannot do without XPath, usually attributes or semantic markup are missing, and the application should be fixed rather than another tool added.
Common mistakes
- Adding a plugin for one button: an extra dependency that a couple of built-in commands replace.
- Rewriting a long XPath into an equally long CSS with indexes: the brittleness stays.
- Searching by text that changes between languages: that case needs
data-cy.
Check yourself
How do you find a button in a table row by the text of a neighbouring cell in Cypress?
Answer
cy.contains('td', 'text').closest('tr').find('button'): find the cell, go up to the row, go down to the button.
Why is the answer "I will add @cypress/xpath" considered weak?
Answer
The plugin is no longer supported, and XPath needs are covered by contains, closest, siblings and data-cy.
cy.contains. Middle/Senior: I move to a parent or sibling with closest, parents, siblings, and for stability I ask for data-cy. Playwright has XPath natively but also recommends role and test id.