Skip to main content

等待

🌐 Waiting

大多数工具都会自动等待:在执行某个操作后,服务器会等待触发的导航和网络活动稳定下来,然后才返回快照。当你需要等待特定的事情时,使用 browser_wait_for

🌐 Most tools wait on their own: after an action, the server waits for triggered navigations and network activity to settle before returning a snapshot. Use browser_wait_for when you need to wait for something specific.

browser_wait_for

等待文本出现或消失,或者等待指定时间过去。必须至少提供 timetexttextGone 中的一个;如果提供了多个,先经过时间,然后是 textGone,最后是 text

🌐 Wait for text to appear or disappear, or for a specified time to pass. At least one of time, text or textGone must be provided; when several are given, the time elapses first, then textGone, then text.

参数类型是否必填描述
time数字等待的时间(秒,最多30秒)
text字符串要等待的文本
textGone字符串要等待消失的文本

等待文本出现

🌐 Wait for text to appear

→ browser_wait_for { text: "Upload complete" }
Waited for Upload complete

等待文字消失

🌐 Wait for text to disappear

→ browser_wait_for { textGone: "Loading..." }
Waited for Loading...

等待固定时间

🌐 Wait a fixed duration

→ browser_wait_for { time: 3 }
Waited for 3

工作流:等待异步操作

🌐 Workflow: waiting for async operations

You: Click the upload button and wait for it to finish.

→ browser_click { target: "e9" }

- progressbar "Uploading..." [ref=e12]

→ browser_wait_for { textGone: "Uploading..." }

- text: Upload complete! File saved.
- link "View file" [ref=e15]

更复杂的情况

🌐 More complex conditions

对于文字匹配无法表达的等待——比如 CSS 选择器、JavaScript 谓词、特定响应——使用 browser_run_code_unsafe

🌐 For waits that text matching can't express — a CSS selector, a JavaScript predicate, a specific response — use browser_run_code_unsafe:

→ browser_run_code_unsafe {
code: "async (page) => { await page.waitForSelector('.data-loaded'); }"
}

→ browser_run_code_unsafe {
code: "async (page) => { await page.waitForResponse(r => r.url().includes('/api/items') && r.ok()); }"
}

操作和导航超时可以通过 --timeout-action(默认 5000 毫秒)和 --timeout-navigation(默认 60000 毫秒)进行配置。

🌐 Action and navigation timeouts are configurable with --timeout-action (5000ms by default) and --timeout-navigation (60000ms by default).