You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress点击按钮后新页面元素无法交互问题求助

解决Cypress中点击按钮打开新页面无法交互的问题

问题核心

你遇到的问题根源在于:【12 Tesouraria】按钮的跳转逻辑并非依赖a标签的target属性,而是通过网页内置的Popup.open()方法触发新窗口打开。因此仅移除target属性完全无效——跳转行为由JS逻辑控制,和a标签默认行为无关。

解决方案:拦截window.open捕获跳转URL,在当前标签页访问

通过重写页面的window.open方法,捕获按钮要打开的新页面URL,直接在当前Cypress控制的标签页中访问该URL,既避免新窗口无法交互的问题,也不会导致重复加载。

修改后的测试代码

import 'cypress-iframe';

describe('Access TES', () => {
  before(() => {
    cy.visit('https://multiportal-hml.paycashprotege.com.br/portal2PN/faces/faces1/index.jsp');

    cy.get('input#frmcaindex\\:NOME_USUARIO', { timeout: 10000 })
      .should('be.visible')
      .type('luirenba')
      .should('have.value', 'xxxxx');

    cy.get('input#frmcaindex\\:SENHA_ATUAL', { timeout: 10000 })
      .should('be.visible')
      .type('xxxxxx')
      .should('have.value', 'xxxxxxx');

    cy.get('#frmcaindex\\:btnOk', { timeout: 10000 })
      .should('be.visible')
      .click();
  });

  it('click button [12 Tesouraria]', () => {
    // 拦截window.open,捕获跳转URL
    let newPageUrl;
    cy.window().then(win => {
      const originalOpen = win.open;
      // 重写open方法,提取目标URL
      win.open = (url) => {
        newPageUrl = url;
        // 返回模拟窗口对象,避免网页调用focus()报错
        return { focus: () => {} };
      };
    });

    // 点击按钮,触发跳转逻辑并捕获URL
    cy.get('a.aht-controle-TS_10315')
      .scrollIntoView()
      .should('be.visible')
      .click();

    // 访问捕获到的目标页面
    cy.wrap(newPageUrl).should('not.be.undefined').then(url => {
      cy.visit(url);
    });

    // 处理frameset中的main frame
    cy.frameLoaded('frame[name="main"]');

    cy.iframe('frame[name="main"]')
      .find('input[type="image"][name="page:frmSideMenu:_id42"]')
      .scrollIntoView()
      .should('be.visible')
      .click();
  });
});

关键说明

  1. 拦截window.open:通过重写该方法,直接获取JS逻辑要打开的URL,无需依赖a标签属性。
  2. 模拟窗口对象:返回包含focus()方法的模拟对象,避免网页代码调用handle.focus()时抛出错误。
  3. 动态访问URL:不再硬编码目标页面地址,测试逻辑更灵活,适配环境变动。

内容的提问来源于stack exchange,提问作者Luisa Renba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:59:52