【问题标题】:Is there a way to let selenium ignore the timeoutexception Error and continue if it happens?有没有办法让 selenium 忽略 timeoutexception 错误并在它发生时继续?
【发布时间】:2021-05-13 23:10:01
【问题描述】:

我在 python 中使用 selenium,我的 chromedriver 和 chrome.exe 是 ver90.0

我遇到了一个问题,即一旦发生超时错误,脚本就会停止,问题是它总是会发生,有时在几个小时内,有时在几分钟内。

它会显示类似:

selenium.common.exceptions.timeoutexception: message: timeout: timed out receiving message from renderer: -0.014

当发生这种情况时,我看到网页通常正在加载并且它基本上无法加载导致发生错误。

有没有办法忽略这个错误,基本上让脚本要么刷新,要么继续加载到列表中的下一个网页。示例代码如下所示:

while True:
     driver.get(thislist[i])
     if .......:
        i = i + 1

我尝试了很多修复,但都没有奏效。我尝试使用 beta chrome.exe 和 chrome webdriver ver 91.0。我还尝试了包含 chrome_options 并且还尝试了一些不在此代码中的标头,但我总是收到超时错误。我之前问过这个问题几次,但没有一个有效的解决方法。

chrome_options.add_argument('--enable-automation')
chrome_options.add_argument('--lang=en')

chrome_options.add_argument("window-size=1920,1080")
chrome_options.headless = True


chrome_options.add_experimental_option ("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(executable_path='C:/Webdriver/chromedriver', chrome_options=chrome_options)
   
     

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    在引发错误的操作周围添加try catch 怎么样。如果有错误,您只需在循环中continue

    使用Exception,因为您不知道将引发哪种异常。


    您始终可以使用以下行将 selenium 的超时设置为更长:driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    【讨论】:

      【解决方案2】:
      while True:
          try:
              driver.get(thislist[i])
          except timeoutexception as e:
              print(e)
              continue
          else:
              # do what you want here
      

      当出现超时异常时,代码只会打印它并继续下一个循环。如果没有,则可以在“else:”下进行网络驱动。

      感谢阅读。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 2010-11-14
        • 2021-03-16
        • 1970-01-01
        相关资源
        最近更新 更多