Skip to content

E2E 测试 Timeout Waiting for 元素

错误信息

                TimeoutError: Waiting for selector 失败之后 30000ms
              

快速摘要

E2E 测试 waited for 元素那个从不 appeared 在内 timeout. page 可能不 有加载, or 元素 doesn't exist.

为什么发生

  • page didn't 加载正确地 (network issue, 错误 URL).

  • 元素 selector 是错误 — typo or 更改 HTML 结构.

  • 元素 appears 之后慢 异步操作那个 exceeds timeout.

  • application 有 bug 那个 prevents 元素从 渲染.

  • 认证 or 导航失败 silently.

最小示例

✗ 错误代码
test('login', async ({ page }) => {
  await page.goto('/login');
  await page.fill('#usr', 'alice');  // wrong selector
  await page.click('#submit');
  await page.waitForSelector('#dashbard'); // typo — never found
});
✓ 修复代码
test('login', async ({ page }) => {
  await page.goto('/login');
  await page.fill('#username', 'alice');
  await page.click('#submit');
  await page.waitForSelector('#dashboard'); // correct selector
});

Fix selector 到 match 实际元素 ID on page.

如何诊断

  • Take screenshot or video 的测试运行到 see 实际 page 状态.

  • 检查如果 page URL 是正确之后导航.

  • 验证 selector matches 实际 HTML (inspect page manually).

  • Increase timeout temporarily 到 see 如果它's 只是慢.

  • 检查 network logs for 失败 API calls 那个 prevent 渲染.

如何修复

  • Fix selector 到 match 实际 HTML 结构.

  • Fix page 导航 or 认证 flow.

  • Increase timeout 如果操作是 legitimately 慢.

  • 添加 explicit waits for 中级状态之前 looking for 最终元素.

  • Fix application bugs 那个 prevent 元素从 渲染.

如何预防

  • Use auto-waiting features (Playwright/Cypress wait automatically for 元素).

  • Use resilient selectors (数据-testid, role-based) 那个 don't break on UI 更改.

  • Take screenshots on 失败 for 调试.

  • 设置 appropriate timeouts for 不同操作.

相关资源

相关课程

← 返回语言