Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the HTML of a hidden element in Cypress?

Tags:

cypress

I have a hidden div on a page I'd like to get a reference to in Cypress so I can get its text and assert on it. As far as I can tell from the API docs I don't see any way to do this. You can use the { force : true } option to force Cypress to click on something it doesn't think it can, but there's no option for that to force Cypress to look for elements that are not visible to the user, but are in the dom.

The element is hidden on the dom by a display: none style. This is just one of the many reasons Cypress will consider an element "hidden" and not find it. https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html#Visibility

I have read through the API docs and mostly tried cypress.get()

like image 557
Jazzepi Avatar asked Oct 24 '25 20:10

Jazzepi


2 Answers

I have created a hidden div in my html, the below test run successfully in Cypress. The display property for that div in css #hiddenDiv1{ display:none; }

describe('Hidden text', function() {
  it.only('Test for the hidden text', function() {
   cy.visit('url goes here')
   cy.get('#hiddenDiv1').invoke('text')
      .then((text)=>{
        const divTxt = text;
        expect(divTxt).to.contain('Life is Beautiful!');
      })

  })
})
like image 76
soccerway Avatar answered Oct 26 '25 11:10

soccerway


When I am searching for something that is hidden, I always just add .should('not.be.visible') to the search, and it picks it up.

Not sure if that's right for you, but hope that helps.

like image 33
Shibby Avatar answered Oct 26 '25 13:10

Shibby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!