我知道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
在打印到PDF之前,我认为Chrome会在上述四个示例中等待0,10,100和1000秒,但实际的等待时间似乎很遥远。我的问题是,在将页面打印为PDF之前,如何让Chrome等待X秒?我目前只考虑使用Chrome CLI,而且我不是在寻找像Puppeteer这样的工具。
我可以轻松回答您的标题问题(这可以解释您的结果)。 --virtual-time-budget,表示进程等待页面加载的时间,而不是它将等待那么长时间。如果请求的结果可用(没有更多的网络请求待处理),它将立即返回结果。
返回的信息应该是正确的,除非混合中有AJAX请求或其他Javascript。如果是这样,您必须使用Javascript / DOM操作来解决问题。