question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

select outer element with cy.contains()

See original GitHub issue

Current behavior:

As you mentioned below we can reach an element with html tags:

https://docs.cypress.io/api/commands/contains.html#Selector

But if a tag name is exists more than one nested it select the innermost one.

screen shot 2018-07-27 at 12 57 32

Test code is below:

cy.contains('tr', 'Semanta Murphy')

Desired behavior:

But i need outer DOM object. How can I select it?

Versions

Cypress 3.0.2 macOS 10 Electron 59

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
jennifer-shehanecommented, Jul 27, 2018

Yes, this is the intended behavior of .contains() when passed a selector - to find the deepest element containing the text, then go up to the first parent matching the selector passed.

Some things that would get you the tr’s parent tr:

cy.contains('tr', 'Semanta Murphy').parent('td').parent('tr')
cy.contains('tr', 'Semanta Murphy').parentsUntil('tr')
cy.contains('tr.ember-view', 'Semanta Murphy')

Let me know if this helps.

0reactions
elcsigacommented, Jun 19, 2020

Also parentsUntil(null, 'tr.ember-view') worked for me. Second parameter is a filter, so if you have a selector which safely matches a single level, it’s fine.

A cy.safeContains() command can be implemented this way:

Cypress.Commands.add('safeContains', (collectionSelector, searchSelector, searchString) => cy
    .get(collectionSelector)
    .contains(searchSelector, searchString)
    .parentsUntil(null, collectionSelector)); 

cy.safeContains('tr', 'td.name', 'foo') then returns a tr element which has the ‘foo’ match in his name column.

Read more comments on GitHub >

github_iconTop Results From Across the Web

contains
.contains() yields the new DOM element it found. .contains() is a query, and it is safe to chain further commands.
Read more >
Cypress get element that a child contains
I'm trying to find in a table my row that cy.contains() a specific text. I ...
Read more >
contains() - an overlooked gem in Cypress
If you are familiar with this command, you probably already know that it helps you select an element using a text: cy .contains('Apples')....
Read more >
Querying | Cypress examples (v9.7.0)
To query for the button, use the cy.get() command. ... Use :contains(text) to find elements with the given text string and :not(:contains(text)) to...
Read more >
Cypress Locators : How to find HTML elements
Working with Multiple Elements in Cypress ; cy.get('.msg-body') might return more than one element, but by using ; last() it will always be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found