为什么 puppeteer page.goto() 挂起? [英] Why puppeteer page.goto() hangs?

查看:364
本文介绍了为什么 puppeteer page.goto() 挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有沙箱的 alpine-chrome 容器中运行 puppeteer,一切都按照文档的建议完成.而且我创建了一大堆不同的由木偶操作者驱动的服务器.

I'm running puppeteer in an alpine-chrome container with sandboxing, everything is done exactly as the docs suggest. And I've created a whole bunch of different puppeteer-fueled servers.

我不带任何参数启动 Puppeteer,除了 headless: false.

I'm launching Puppeteer without any arguments, except headless: false.

为什么 await page.goto() 方法这么有问题?它不断给我各种无意义的问题.

Why is await page.goto() method so problematic? It keeps giving me all kinds of non-sensical problems.

我总是这样做:

await page.goto('https://my-url.com/something', {
  waitUntil: 'networkidle2',
  timeout: 0
})

现在它只是随机挂起.什么也没发生,代码执行就停在那里.我所看到的是,与正常进行的执行相比,CPU 利用率达到顶峰,然后下降到略微升高的水平.90% 的时间 goto 根本不会提高 CPU 利用率.同一个页面,同一个服务器,在同一个脚本执行中随机挂起.

Right now it just randomly hangs. Nothing happens, code execution just stops there. All I see is that CPU utilization hits the roof and then down to slightly-elevated levels, comparing to the properly-ongoing execution. 90% of time the goto doesn't elevate CPU utilization at all. Same page, same server, hangs randomly within the same script execution.

为什么会这样?我是否必须在每个 page.goto() 方法周围的代码中放置 setTimeouttry-catch 语句?感觉就像我使用 puppeteer 从根本上不正确,因为这种行为没有意义.

Why is this hapening? Do I have to put setTimeout and try-catch statements all over my code around each page.goto() method? It feels like I'm using puppeteer fundamentally incorrect, because this behaviour doesn't make sense.

抱歉问的含糊不清,我不明白这里有哪些信息会有所帮助.

Sorry for vague question, I don't understand what kind of information would be helpful here.

推荐答案

看起来 goto 方法抛出了错误.因此,从 puppeteer 文档中查看这些信息非常重要 -

It looks like the goto method is throwing you errors. Therefore it's important to review this tidbit of info from puppeteer documentation -

https://devdocs.io/puppeteer/index#pagegotourl-options

page.goto 将在以下情况下抛出错误:

page.goto will throw an error if:

-存在 SSL 错误(例如在自签名证书的情况下).

-there's an SSL error (e.g. in case of self-signed certificates).

-目标网址无效.导航期间超时.

-target URL is invalid. the timeout is exceeded during navigation.

-远程服务器没有响应或无法访问.

-the remote server does not respond or is unreachable.

-主要资源加载失败.

我还建议尝试 networkidle0 并且可能不传递超时参数.您遇到这个问题很奇怪,因为它通常不是 page.goto() 函数给我的问题,而是使用选择器.

I would also recommend trying networkidle0 and maybe not passing a timeout parameter. It's odd you're having this issue as normally its not page.goto() function giving me issues but working with the selectors.

这篇关于为什么 puppeteer page.goto() 挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆