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

使用cy.origin测试多域名失败:无法定位#login_field元素求助

解决cy.origin中无法找到#login_field元素的问题

以下是几个针对性的排查和修复方案:

  • 确保cy.origin在页面跳转完成后执行
    点击GitHub登录按钮后,页面需要时间完成跨域跳转,提前进入cy.origin会导致元素未加载。添加URL断言等待跳转完成:
cy.contains("GitHub").click({ force: true });
// 等待URL包含github域名,确认跳转完成
cy.url().should('include', 'github.com');

cy.origin("https://github.com", () => {
  // 登录操作
});
  • 在cy.origin内等待元素可见再操作
    避免直接操作未渲染完成的元素,添加可见性断言确保元素存在:
cy.origin("https://github.com", () => {
  cy.get('#login_field').should('be.visible').type("*****@gmail.com");
  cy.get("#password").should('be.visible').type("********");
  cy.get('input[type="submit"]').should('be.visible').click();
});
  • 修正"Sign in"按钮的选择器
    原代码中cy.get("input").contains("Sign in")的匹配逻辑不可靠,input按钮的文本通常存储在value属性中,改用更精准的选择器:
// 替换原按钮点击代码
cy.get('input[type="submit"][value="Sign in"]').click();
  • 验证目标origin的正确性
    如果跳转后的GitHub登录页面使用了子域名(比如https://auth.github.com),需要将cy.origin的参数改为实际的origin(协议+主机)。可以通过打印URL确认:
cy.contains("GitHub").click({ force: true });
cy.url().then(url => {
  console.log('实际跳转URL:', url); // 查看真实origin
});

内容的提问来源于stack exchange,提问作者Mr.Prasad J

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 19:04:05