硒常见异常超时异常消息:超时错误

时间:2018-08-15 04:20:10

标签: python selenium

我一直在研究OkCupid的在线状态检查脚本。所以发生了什么,如果我监视的人登录OkCupid,脚本将基于<div class="userinfo2015-basics-username-online-icon isOnline"></div>对其进行检测,并在登录后显示。它每分钟循环一次,以查看是否有任何状态更改。在大多数情况下,它可以正常运行30-40分钟,但随后崩溃。 脚本的输出如下所示:

----------------------------------------------
[i] Status   |   Last Checked   |   Update   |
----------------------------------------------
[-] Offline      08-14 18:33:03     60 Sec   |
[+] Online       08-14 18:34:05     60 Sec   |
[+] Online       08-14 18:35:09     60 Sec   |
[+] Online       08-14 18:36:10     60 Sec   |
----------------------------------------------

这是循环:

try:
while 1:
        status = driver.find_elements_by_tag_name('div')[41].get_attribute('class') # Span Class for Online status
        driver.refresh()
        time_online = datetime.now().strftime('%m-%d %H:%M:%S')
        time.sleep(5)
        if status == str('userinfo2015-basics-username-online-icon isOnline'):
            print('[+]  Online'+7*' '+time_online+5*' '+str(update+5)+' Sec   |')
        elif status != str('userinfo2015-basics-username-online-icon isOnline'):
            print(bc.red+'[-] Offline'+6*' '+time_online+5*' '+str(update+5)+' Sec   |')
        time.sleep(update)
except KeyboardInterrupt:
        print('  Exiting\n'+46*'-')
driver.quit()

然后出现错误:

Traceback (most recent call last):
File "okCupid_online_status.py", line 38, in <module>
    status = driver.find_elements_by_tag_name('div')[41].get_attribute('class') # Span Class for Online status
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 539, in find_elements_by_tag_name
    return self.find_elements(by=By.TAG_NAME, value=name)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 985, in find_elements
    'value': value})['value'] or []
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf),platform=Linux 4.15.0-29-generic x86_64)

所以我更想知道是否可以引发异常并绕过错误(如果发生这种情况),并让我的脚本继续循环?

1 个答案:

答案 0 :(得分:0)

很难确定,但是根据我的经验,Selenium中的Timeout异常通常在浏览器页面没有响应时发生。下次发生这种情况时,建议您检查是否是这种情况。

如果确实是这种情况,则由于要创建刮板(与测试自动化相对),因此应捕获异常并重试。如果您要编写自动化的测试,我会说您应该报告有关此问题的错误,因为无响应的页面通常是该页面的JavaScript代码中的错误。