使 Selenium 在超时错误后继续

Make Selenium continue after a timeout error

我有一个代码 returns 网站列表的标题。有时,一个网站需要花费大量的时间来加载,所以当这种情况发生时,会出现超时错误提示。我想这样做,以便在发生此类错误时,程序继续 运行 而不是完全停止。

密码是:

from pyvirtualdisplay import Display
from time import sleep
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
display = Display(visible=0, size(800,600))
display.start()
driver = webdriver.Firefox(executable_path='/usr/local/lib/geckodriver/geckodriver')
driver.set_page_load_timeout(60)
driver.get('https://google.com')
print(driver.title)

以下代码是在 60 秒后页面未加载时发生超时的原因:

driver.set_page_load_timeout(60)

60秒后,程序停止,提示超时错误。我希望它继续下一个 url。

您可以使用 try except 来处理和传递任何错误。

from pyvirtualdisplay import Display
from time import sleep
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

display = Display(visible=0, size(800,600))

display.start()

driver = webdriver.Firefox(executable_path='/usr/local/lib/geckodriver/geckodriver')


try:
    driver.set_page_load_timeout(60)
except Exception as e:
    print(e)

driver.get('https://google.com')

print(driver.title)

即使出现 page_load_timeout 错误,要遍历 List url,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    
    urls = ["https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl", "https://www.google.com/"]
    driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.set_page_load_timeout(2)
    for url in urls:
        try :
            driver.get(url)
            print("URL successfully Accessed ... Proceeding with other tasks !!!")
            # perform other operations within the url
        except TimeoutException as e:
            print("Page load Timeout Occured ... moving to next item !!!")
    driver.quit()
    
  • 控制台输出:

    Page load Timeout Occured ... moving to next item !!!
    Page load Timeout Occured ... moving to next item !!!
    
  • 注:

    • set_page_load_timeout(2)用于重现页面加载超时,仅供演示。
    • urlsList 仅用于演示目的。

You can find a detailed discussion on page load timeout in