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

使用Playwright实现JS登录时遇「等待导航完成」卡顿问题求助

解决Playwright登录时卡在waiting for scheduled navigations to finish的问题

以下是针对你遇到的问题的具体排查和解决方法:

  • 调整点击操作的等待策略
    Playwright默认会等待点击触发的页面导航完成,但部分页面的登录按钮可能是通过异步请求完成登录(无页面跳转),或导航触发逻辑延迟,导致工具一直等待。可以修改点击的等待参数:

    // 等待网络空闲后结束等待
    await page.locator('#idPasswordInputForm a.input_area_btn_01').click({
      timeout: 30000,
      waitUntil: 'networkidle'
    });
    
    // 若确认点击后无页面跳转,直接跳过等待
    await page.locator('#idPasswordInputForm a.input_area_btn_01').click({
      timeout: 30000,
      noWaitAfter: true
    });
    
  • 改用表单提交方式触发登录
    如果登录逻辑是通过表单提交实现的,直接调用表单的submit()方法比点击a标签更可靠,Playwright能更好地识别表单提交后的导航行为:

    await page.locator('#idPasswordInputForm').submit();
    
  • 提前等待页面加载完成
    手动登录正常但自动化卡住,可能是页面存在未完成的异步请求或加载元素,导致Playwright误判导航状态。可以在点击前等待页面进入稳定状态:

    // 等待网络请求全部完成
    await page.waitForLoadState('networkidle');
    // 或等待加载动画消失(替换为你页面的加载元素选择器)
    await page.locator('.loading').waitFor({ state: 'hidden' });
    
    await page.locator('#idPasswordInputForm a.input_area_btn_01').click({timeout: 30000});
    
  • 验证元素定位准确性
    确认你的locator是否唯一且元素状态正常,避免定位到不可点击的元素:

    const loginBtn = page.locator('#idPasswordInputForm a.input_area_btn_01');
    // 验证元素可见且可点击
    await expect(loginBtn).toBeVisible();
    await expect(loginBtn).toBeEnabled();
    await loginBtn.click({timeout: 30000});
    

内容的提问来源于stack exchange,提问作者Tung Le

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 15:46:04