select outer element with cy.contains()
See original GitHub issueCurrent 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.
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:
- Created 5 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Let me know if this helps.
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:cy.safeContains('tr', 'td.name', 'foo')
then returns a tr element which has the ‘foo’ match in his name column.