How does cy.get() differ from .find() and cy.contains()?
cy.get() searches from the document root (scoped inside .within()), .find() among descendants of the previous subject, cy.contains() by text. All three are queries and retry until the timeout. cy.get().get() ignores the previous subject, so nested lookups use .find() or .within(). cy.contains() is case-sensitive and without a selector may hit an element other than the button, so it is combined with a selector.
Cypress · Locators and DOM
The page has three orders; the test checks the price of one but picks every price on the page. Why?
A page shows several orders, each with a [data-cy=price]. The test writes cy.get(order).get(price), and the assertion sometimes fails and sometimes passes by accident. This lesson explains where the search scope of cy.get, .find and cy.contains comes from and how to pick the right command.
How it works and why. cy.get() is a “parent” command: it always starts the search at the document root, not at the previous element of the chain. The exception: inside a .within() block (or with the withinSubject option) the root narrows to the given element. .find() is a “child” command and searches only the descendants of the subject. cy.contains() searches by text; in a chain (.contains(...)) the scope is limited to the subject, while a cy.contains(...) at the start of a chain searches the whole document. All three are queries, so they are retried until the timeout. If the DOM was redrawn between commands, Cypress re-queries the chain of queries from its start, so “stale” elements as in Selenium are rare. One limit: an assertion in the middle of the chain locks the subject, and after it Cypress can no longer requery the page.
Where each command searches
Example: the price of the “wrong” order.
cy.get().get()
The second get ignores the previous subject and returns the prices of every order on the page. A have.text assertion on several elements compares the joined text and fails, or passes when there is a single order.
cy.get().find()
find takes only the prices inside the found order. The result does not depend on how many orders the page has.
// searches inside order #1042
cy.contains('[data-cy=order]', '#1042')
.find('[data-cy=price]')
.should('have.text', '$1,200');
// get after get searches the whole page
cy.get('[data-cy=order]').get('[data-cy=price]');
// contains with a selector: the button, not any text
cy.contains('button', 'Place order').click();
Here cy.contains(selector, text) found the order by its text and find narrowed the search to its price. This combination relies on content, not on the order of rows.
Limits and alternatives. According to the docs, cy.contains is case-sensitive (the matchCase: false option turns that off) and also accepts a regular expression. If several elements match the text it yields one: input[type=submit], button, a and label are preferred, otherwise the deepest element. So cy.contains('Save') without a selector may land on something other than the button. Text is sensitive to copy and localisation changes, so combine it with data-cy for stability.
page.locator() searches the page, locator.locator() inside another locator, text is getByText(). The difference is strict mode: if a locator for an action finds several elements, Playwright fails at once. Cypress also needs a single element for click (unless multiple: true), but should happily works with a set.
Common mistakes.
.get()inside a chain instead of.find(): the search covers the whole page, so the test is green only while there is a single such element.cy.contains('Text')without a selector on a page where the text is both in a heading and in a button: Cypress picks by its own priority, not by your intent.- Keeping a subject in a variable:
const order = cy.get(...)stores nothing, because commands are only queued.
Check yourself
What does cy.get('[data-cy=order]').get('[data-cy=price]') find when there are three orders?
Answer
All three prices: the second get searches from the document root and ignores the previous subject. To take the price of one order you need .find() or .within().
Why is cy.contains('button', 'Place order') better than cy.contains('Place order')?
Answer
The selector limits the search to elements of a given kind. Without it a heading or paragraph with the same text may match and the click goes elsewhere.