繁体   English   中英

在同一选项卡中打开 iframe 中的链接

[英]open a link within an iframe in the same tab

我正在尝试单击赛普拉斯 iframe 中的链接。 链接在下一个选项卡中打开,测试失败。

cy.get('iframe.body').wait(1000).its('0.contentDocument.body')
  .find('a').contains('Click').click();

要解决此问题,请使用类似这样的方法使新页面在同一选项卡上打开。 但它不起作用:

cy.get('iframe.body').wait(1000).its('0.contentDocument.body')
  .find('a').contains('Click')
  .invoke('removeAttr', 'target').click();

元素看起来像这样:

<iframe class="body" data-flexie-id="3" data-flexie-parent="true" src="messages/1.html" style="">
</iframe>
<p>
  <a href="https://Thisisalink/91cf-a6c3792a5cc5" target="_blank">Click</a>
</p>

target="_parent"在与父级相同的页面中打开链接。 编辑:我想你不希望<a><iframe>中,对吧?

<iframe class="body" data-flexie-id="3" data-flexie-parent="true" src="messages/1.html" style=""></iframe>
<p><a href="https://Thisisalink/91cf-a6c3792a5cc5" target="_blank">Click</a></p>

我找到了 2 个解决方案:

  1. 复制链接并访问它:

Cypress.Commands.add("ui_clickTagButton", (buttonSelector) => {
    cy.log('ui_clickTagButton - start')

    cy.wrap(buttonSelector)
        .should('have.prop', 'href')
        .then(href => cy.visit(href))

    cy.log('ui_clickTagButton - End')
})
  1. 使用 Jquery删除目标属性- 合并在 cypress 中,然后单击:
Cypress.Commands.add("ui_removeATagTargetAttributeAndCick", (aTagSelector) => {
    cy.log('ui_clickAnchorTagButton')
    cy.get(aTagSelector)
        .then((aTagElement) => {
            Cypress.$(aTagElement).removeAttr("target");
            cy.get(aTagElement)
                .click({force: true})
                .should('not.exist')
        })
})

因此,对于您的情况,我将使用第二个:

cy.get('iframe.body').wait(1000).its('0.contentDocument.body').find('a')
   .then(button => {
       cy.ui_removeATagTargetAttributeAndCick(button)
   })

您的 HTML 表示链接在iframe之外,如果是这样,您不需要 select 它。

使用.find('a')类似于.within(() =>...) ,但由于<a>不在iframe内,如果您直接针对<a> ,您删除目标的策略将起作用

cy.contains('a', 'Click')  
  .invoke('removeAttr', 'target')
  .click();

暂无
暂无

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

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