Selenium设置页面超时时间-快速终止页面加载

当使用Selenium爬取一些页面时,有些页面加载速度特别慢,而我们又不需要等待页面完全加载完毕。
此时可以通过driver.set_page_load_timeout()来设置页面超时时间。
捕获异常,并执行js脚本window.stop()即可实现,代码如下。

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_page_load_timeout(3)

try:
    driver.get('https://hk.louisvuitton.com/zht-hk/homepage')
    print('finish load ....')
except Exception:
    driver.execute_script('window.stop()')
    print(driver.title)
finally:
    driver.quit()

报错处理:
如果遇到如下,超时错误,可以尝试更新chromedriver驱动。

原文地址:https://www.cnblogs.com/superhin/p/12333800.html