jackwener / jackwener/xhs-cli

headless 环境下所有浏览器命令因首页 domcontentloaded 等待超时失败

Open Beginner friendly
#15 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
658
Forks
68
PR merge metrics
No merged PRs in 30d

Description

## Bug: headless 环境下所有浏览器命令因首页 `domcontentloaded` 等待超时失败

**Describe the bug**

在 headless(无头)运行环境中,`xhs whoami` / `xhs feed` / `xhs search` 等所有需要启动浏览器的命令都会在 `client.start()` 阶段失败:

```
❌ Failed to get profile: Page.goto: Timeout 20000ms exceeded.
Call log:
- navigating to "https://www.xiaohongshu.com/", waiting until "domcontentloaded"
```

根因在 `client.py` 的 `start()`:

```python
self._goto(
"https://www.xiaohongshu.com",
timeout=20000,
wait_min=1,
wait_max=2,
context="establishing browser session",
)
```

默认 `wait_until="domcontentloaded"` 且超时固定 20s。在小红书首页 + headless 冷启动场景下,`domcontentloaded` 经常需要 10~20s 甚至更久,加上 camoufox 冷启动开销,很容易撞上 20s 上限。

**To Reproduce**

1. 无头环境安装 xhs-cli 0.1.4 并完成登录
2. 运行 `xhs whoami` 或 `xhs feed`
3. 观察 `Page.goto: Timeout 20000ms exceeded`

**Expected behavior**

- 首页初始化应该用更宽松的超时(例如 40s+),或者
- 允许通过环境变量 / CLI 参数 / 配置文件覆盖 `timeout` 与 `wait_until`

**实测数据(有头/无头对照)**

| 方式 | wait_until | 耗时 |
|---|---|---|
| camoufox 手动导航 `https://www.xiaohongshu.com/` | `commit` | ~1.2s |
| camoufox 手动导航 `https://www.xiaohongshu.com/` | `domcontentloaded` | ~9.1s |

站点本身连通性正常(HTTP 302,connect <0.1s),问题在等待策略而非网络。

**Suggested fix**

1. 最简:`start()` 中把超时从 20000 提高到 40000;
2. 更好:`XhsClient.__init__(timeout=...)` 支持传入或读取环境变量(如 `XHS_CLI_NAV_TIMEOUT` / `XHS_CLI_WAIT_UNTIL`),默认值保持 20000/`domcontentloaded` 以兼容现有行为。

**Environment**

- xhs-cli: 0.1.4
- OS: Debian 13 (Linux)
- Python: 3.11
- Browser: camoufox headless

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in client.py at XhsClient.start() and its _goto call, then reproduce the failure with a headless xhs whoami or xhs feed run. Check the existing timeout and wait_until behavior, and verify that the chosen change lets browser-starting commands complete without the homepage navigation timing out.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.