Cypress not able to locate element using cy.get
See original GitHub issueCurrent behavior
In the below DOM we are trying to locate the heading using data-test-id but for some reason, it is not able to locate in the runtime but when search with the same selector is suggested selector it is able to uniquely identify.
I am using cypress for other #tests where modal is used and I did not find this issue there but in this specific case it’s failing.
Desired behavior
Test code to reproduce
it.only(`Verify user is able to use welcome new member template`, () => {
cy.getDataTestId(`template-link-post-composer`)
.should(`be.visible`)
.should(`contain.text`, `Use a template`)
.click();
cy.getDataTestId(`heading-template-pop-up`).should(`be.visible`).should(`contain.text`, `Use a template`);
cy.getDataTestId(`button-close-template-pop-up`).should(`be.visible`).click();
cy.getDataTestId(`template-link-post-composer`)
.should(`be.visible`)
.should(`contain.text`, `Use a template`)
.click();
cy.getDataTestId(`template-welcome-new-members`)
.should(`be.visible`)
.within(() => {
cy.getDataTestId(`heading-template-type`)
.should(`contain.text`, `Welcome New Members (weekly)`)
.should(`be.visible`);
cy.getDataTestId(`description-template`)
.should(`contain.text`, `Copy this post and use this weekly to welcome the new members of your group`)
.should(`be.visible`);
cy.getDataTestId(`button-view-template`).should(`be.visible`).should(`contain.text`, `View`).click();
cy.getDataTestId(`view-template-pop`).should(`be.visible`);
cy.get('[data-test-id="view-template-heading"]').should(`be.visible`);
cy.getDataTestId(`view-template-heading`).should(`be.visible`).should(`contain.text`, `View template`);
cy.getDataTestId(`template-name-view-pop-up`)
.should(`be.visible`)
.should(`contain.text`, `Welcome New Members (weekly)`);
});
});
Versions
Cypress version - 6.2.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Not able to find the element in cypress - Stack Overflow
no it does not work, it says - CypressError: cy.click() can only be called on a single element. Your subject contained 6 elements....
Read more >find | Cypress Documentation
Get the descendent DOM elements of a specific selector. The querying behavior of this command matches exactly how .find() works in jQuery. Syntax...
Read more >not - Cypress Documentation
Yields .not() yields the new DOM element(s) it found. .not() is a query, and it is safe to chain further commands. Examples. Selector....
Read more >get | Cypress Documentation
The cy.get command always starts its search from the cy.root element. In most cases, it is the document element, unless used inside the ......
Read more >Interacting with Elements - Cypress Documentation
Whenever Cypress cannot interact with an element, it could fail at any of the above steps. You will usually get an error explaining...
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 Free
Top 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
For others who stumble across this issue, as I was suffering from the same exact problem. The suggestion to use
withinSubject:null
from @myselfsrjn4u does work although to add more color from the docs this does:Which clued us in to the mistake we were doing in code - it is due to checking for the existence of the modal in a
within()
block. This has cypress searching the DOM from that point but the modal is outside of it. So an alternative to usingwithinSubject:null
is to check for existence outside of thewithin()
block.@Ppkd2021 Can you try with this —> cy.get(‘[data-test-id=“view-templete-heading”]’,{withinSubject:null})