Search code examples
iframecypresscypress-iframe

I have two iframes I need one


How do I take an iframe if I have two with the same classes and neither eq() nor first() works when I use cy.iframe().

Here is the error:

Each radio is made up of a 'form' and inside each one contains the respective iframe.
But I only want to take the one that is checked

enter image description here*

This is my script

enter image description here


Solution

  • I'm guessing you're using cypress-iframe since cypress doesn't have a built-in iframe command available.

    As you can tell, iframe() allows passing a selector. If your iframe tags are identical in their attributes so you cannot use attributes to select, or you just want to use the order, note that you have to use that as part of the selector. Instead use a selector that has the order part of it such as :first or :nth-child(n)

    // will NOT work
    cy.iframe().first()
    cy.iframe().eq(1)
    
    // will work
    cy.iframe("iframe:first")
    // for 2nd item use 2, since it uses a 1 based index
    cy.iframe("iframe:nth-child(2)")