Chrome Webdriver 在 Selenium 中产生超时

Hal*_*rst 1 selenium google-chrome webdriver

我正在尝试使用 Selenium 和 Chrome 的 webdriver 抓取网站,这一切都很好,直到我切换到更新的 Macbook。突然之间,webdriver 似乎无法识别网站何时真正完全加载。

错误信息如下

TimeoutException:消息:超时:无法根据超时确定加载状态:从渲染器接收消息超时:
-0.003(会话信息:chrome=54.0.2840.87)(驱动程序信息:chromedriver=2.25.426935(820a95b0b81d33e42712f912712f912712f91298c) 10.12.1 x86_64)

我的代码如下所示:

      import os
      import time
      from selenium import webdriver
      driver = webdriver.Chrome(os.path.join(os.getcwd(), 'chromedriver'))
      driver.get('http://www.clever-tanken.de/')
Run Code Online (Sandbox Code Playgroud)

小智 5

这是 chrome webdriver 中的一个已知错误。EX1练习2

经过非常简短的阅读后,看起来他们的开发人员在重现该错误时遇到了一些麻烦。@dimkin 提到 CDN 可能是导致该错误的原因。看起来其他人已经报告了这个错误,并有类似的怀疑,认为 cdn 内容没有正确解析 DNS。

我能够隔离问题。在我的情况下,它只发生在我的自定义本地 cdn 域上引用静态文件(HTML 标记中的图像,例如http://cdn.local.myproject.net/static/myimage.png)的页面上。如果我使用相对路径“/static/myimage.png”或本地主机“ http://127.0.0.1/static/myimage.png ”,则不存在,所以我认为这是一个DNS问题。

我能够通过使用--dns-prefetch-disable chrome 选项绕过这个问题。

例子:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--dns-prefetch-disable")
driver = webdriver.Chrome(chrome_options=chrome_options)
Run Code Online (Sandbox Code Playgroud)

提到的另一个解决方法看起来很有希望。它不能解决任何问题,但可能会解决问题。它捕获异常并简单地尝试刷新:

你需要: from selenium.common.exceptions import TimeoutException

try:
   webDriver.get(url);
except TimeoutException as ex:
   print(ex.Message)
   webDriver.navigate().refresh()
Run Code Online (Sandbox Code Playgroud)