gpt4 book ai didi

javascript - Cypress 将一个 iframe 访问到另一个 iframe

转载 作者:行者123 更新时间:2023-12-01 23:43:48 57 4
gpt4 key购买 nike

我正在尝试找到一种解决方案,以使用 cypress 将 iframe 访问到 iframe 中。我已经尝试了以下所有功能但均未成功:

Cypress.Commands.add('getIframeBody', (iframeSelector) => {
// get the iframe > document > body
// and retry until the body element is not empty
return cy
.get(iframeSelector)
.its('0.contentDocument.body').should('not.be.empty')
// wraps "body" DOM element to allow
// chaining more Cypress commands, like ".find(...)"
// https://on.cypress.io/wrap
.then(cy.wrap)
})

Cypress.Commands.add('iframev3', { prevSubject: 'element' }, (iframeSelector, callback) => {
// For more info on targeting inside iframes refer to this GitHub issue:
// https://github.com/cypress-io/cypress/issues/136
cy.log('Getting iframe body')

return cy
.get(iframeSelector)
.wrap($iframe)
.should(iframe => expect(iframe.contents().find('body')).to.exist)
.then(iframe => cy.wrap(iframe.contents().find('body')))
.within({}, callback)
})

Cypress.Commands.add('iframeV4', { prevSubject: 'element' }, $iframe => {
return new Cypress.Promise(resolve => {
$iframe.ready(function() {
resolve($iframe.contents().find('body'));
});
});
});

我也试过 iframe 插件。

我试着用这个方法访问它,例如:

cy.get('#m_c_layoutElem_cmsdesktop').iframeV4()
.get('#m_c_layoutElem_contentview', {timeout: 10 * 1000}).iframeV4().debug()

但它总是一样的,我能够抓取第一个 iframe 但不能抓取第二个!

问候,

编辑:感谢当前的评论,我进步了一点,这是当前状态:

cy.getIframeBody('#m_c_layoutElem_cmsdesktop')
.then(iframe1 => {
let iframe2 = iframe1.find('#m_c_layoutElem_contentview')[0] //Do stuff with queryselector()
console.log(iframe2);
let iframe3 = iframe2.contentWindow.document.querySelector('#m_c_plc_lt_ctl00_HorizontalTabs_l_c');
console.log('iframe3', iframe3) // This is set to an iframe
console.log(iframe3.contentWindow) //This is null
//debugger

/*let cyIframe3 = Cypress.$(iframe3.contentWindow.document);
console.log('cy iframe3', cyIframe3);
*/
let cyIframe32 = cy.get(iframe3);
console.log('cy iframe32', cyIframe32); //This is: $Chainer {userInvocationStack: " at Context.eval (http://localhost:8090/__cypress/tests?p=cypress\support\index.js:237:25)", specWindow: Window, chainerId: "chainer1656", firstCall: false, useInitialStack: false}

/*let field = iframe3.contentWindow.document.querySelector('#m$c$f$Title$txtText'); // Exception because iframe3.contentWindow is null
console.log(field);
*/
iframe3.find('#m$c$f$Title$txtText').type('Coucou'); //fin is not a function
});

最佳答案

不知道这是否对您有帮助,或者您是否已经修复了它,但我确实找到了在另一个 iframe 中访问 iframe 的解决方案,因为我试图自己解决这个问题,我会留下答案在这里,希望它能有所帮助。

因此,iframe 的 id 示例:

  • iframe1, id='first_iframe"
  • iframe2, id='second_iframe"

这是您可以尝试的代码示例:

cy.get('#first_iframe').then($firstIframe => {
const $secondIframeReference = $firstIframe.contents().find('#second_iframe');

cy.wrap($secondIframeReference).as('secondIframeReference'); // Saving this as a reference so we can access it again using get command

// Now we are accessing the second iframe
cy.get('@secondIframeReference').then($secondIframe => {
const $elementYouWant = $secondIframe.contents().find('element you want to interact with');

cy.wrap('$elementYouWant').type("It's writing some stuff"); // In case it is an input field, for example
});
});

阅读此 article 后进入此解决方案

关于javascript - Cypress 将一个 iframe 访问到另一个 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64606194/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com