cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.03k stars 3.19k forks source link

Not able to find elements within the body of iframe, getting Timed Out Error #15676

Closed sandyklh closed 1 year ago

sandyklh commented 3 years ago

Hi , I am new to cypress , and i am stuck with using frames

Current behavior

i am trying to find an element within the iframe but could not find it. Giving error as Timed out after 8000s. Expected element not found. The concern is its not able to locate the item even after wrapping the body inside the frame. i am using the cypress-iframe plugin. Trying to click on the button with frame id portalFrame

Desired behavior

As per expectations it should be able to click on button present within the frame.

Test code to reproduce

  cy.frameLoaded('#portalFrame')
        cy.iframe().find('a[href*="cashlanding"]').eq(0).click({force: true})

        cy.iframe().find('a[href*="statementCredit.htm"]').click()

Error Msg:

Timed out retrying after 8000ms: Expected to find element: a[href*="statementCredit.htm"], but never found it. Queried from element:

Versions

Version: 6.8.0

HTML:

image

image

symon-skelly commented 3 years ago

Hi @sandyklh, I've had problems with Iframes and cypress in the past. this is the solution I have been using for months now and it's worked for any Iframe I've encountered so far:

Function:

const getIframeBody = (iframe) => {
    //gets the body of the iframe and wraps it in a cypress understandable object
    return cy
    .get(iframe, { log: false })
        .its('0.contentDocument.body', { log: false }).should('not.be.empty')
        .then((body) => cy.wrap(body, { log: false }))
}

Usage:

getIframeBody('ifr_name').find('desired_selector').click() //replace 'ifr_name' with your Iframe selector

For you specifically: cy.iframe().find('a[href*="cashlanding"]').eq(0).click({force: true}) becomes: getIframeBody('#portalFrame').find('a[href*="cashlanding"]').eq(0).click()

sandyklh commented 3 years ago

@symon-skelly i can get to the first frame i.e. getIframeBody('#portalFrame').find('a[href="cashlanding"]').eq(0).click() but on the next page i need to click on the highlighted element above i am using getIframeBody('#portalFrame').find('a[href="statementCredit.htm"]').eq(0).click() but again i am getting error

as

Timed out retrying after 8000ms: Expected to find element: a[href*="statementCredit.htm"], but never found it. Queried from element:

image

Please help!!! Thanks in Advance!!

cypress-app-bot commented 1 year ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot commented 1 year ago

This issue has been closed due to inactivity.