Chrome CLI 的参数 --virtual-time-budget 到底是什么意思?

What does the argument --virtual-time-budget of Chrome CLI really mean?

我知道 Chromium 的参数 --virtual-time-budget in the source 的文档,但我不觉得我理解它:

// If set the system waits the specified number of virtual milliseconds before
// deeming the page to be ready.  For determinism virtual time does not advance
// while there are pending network fetches (i.e no timers will fire). Once all
// network fetches have completed, timers fire and if the system runs out of
// virtual time is fastforwarded so the next timer fires immediately, until the
// specified virtual time budget is exhausted.
const char kVirtualTimeBudget[] = "virtual-time-budget";

我做了一些实验,结果让我感到困惑:

# I'm on macOS; you may change this alias according to your own OS
$ alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
$ chrome --version
Google Chrome 70.0.3538.110

$ time chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
real    0m0.912s
user    0m0.264s
sys     0m0.219s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=10000 https://www.chromestatus.com/
real    0m2.502s
user    0m0.347s
sys     0m0.244s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=100000 https://www.chromestatus.com/
real    0m15.432s
user    0m0.759s
sys     0m0.406s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=1000000 https://www.chromestatus.com/
real    0m15.755s
user    0m0.755s
sys     0m0.401s

我以为Chrome会在上面四个例子中等待0秒、10秒、100秒、1000秒才打印成PDF,但实际等待时间似乎相差甚远。我的问题是,如何让 Chrome 在将页面打印为 PDF 之前明确等待 X 秒?我目前只考虑 Chrome CLI,我不会寻找像 Puppeteer 这样的工具。

我可以很容易地回答你的标题问题(这解释了你的结果)。 --virtual-time-budget,表示进程等待页面加载多长时间,而不是等待那么久。如果请求的结果可用(没有更多的网络请求挂起),它将立即 return 结果。

所输入的信息 return 应该是正确的,除非有 AJAX 请求或其他 Javascript 的要求。如果是这样,您必须诉诸 Javascript/DOM 操作来解决问题。