简体   繁体   English

单击 iFrame 内的元素 - 赛普拉斯

[英]Click on element inside iFrame - Cypress

Can anyone, please assist in following:任何人都可以,请协助以下:

Want to click element which resides on found iframe.想要单击位于找到的 iframe 上的元素。 Also, imported plugin import 'cypress-iframe';另外,导入的插件import 'cypress-iframe'; in commands class.命令类中。 Code for founding iframe and clicking on element:创建 iframe 并点击元素的代码:

clickIFrameElement(elementCss, element1Css) {
    cy.frameLoaded(elementCss);
    cy.iframe(elementCss).find(element1Css).then(function(){
       cy.get(element1Css).click({ force: true })
    })
}

In test, I call this command:在测试中,我调用这个命令:

cy.frameLoaded(elementCss).eq(0);
cy.iframe().find(element1Css).should('be.visible').click()

It does find iframe, but do not click on element.它确实找到了 iframe,但不点击元素。 Error which get:得到的错误:

Timed out retrying after 20000ms: Expected to find element: #grouped-pageload-Banner button[class*="save-consents evSpAcceptBtn"], but never found it.

HTML for iframe:用于 iframe 的 HTML: 在此处输入图像描述

What I am doing wrong, or what is incorrect in code?我做错了什么,或者代码中有什么不正确?

Thank you in advance先感谢您

UPDATE更新

Tried also second approach with following command:还使用以下命令尝试了第二种方法:

cy.iframe().find(#grouped-pageload-Banner button[class*="save-consents evSpAcceptBtn"]).should('be.visible').click()

But got second error:但得到第二个错误:

cypress-iframe commands can only be applied to exactly one iframe at a time.  Instead found 7

Please replace this line into your code请将此行替换为您的代码

cy.iframe('#ifrmCookieBanner')
    .find('#grouped-pageload-Banner button[class*="save-consents evSpAcceptBtn"]')
    .should('be.visible')
    .click()

And put this in your support/index.js并将其放入您的support/index.js

Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
return false
})

Check if the iframe URL belongs to the same domain of current window, otherwise, this cannot be executed for security reasons.检查 iframe URL 是否属于当前窗口的同一个域,否则出于安全原因无法执行。 But if you have how to install a Chrome extension in user, this action can be performed via content_script ;)但是,如果您有如何在用户中安装 Chrome 扩展程序,则可以通过 content_script 执行此操作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM