1

我有一个返回网站列表标题的代码。有时,一个网站的加载时间太长了,所以当这种情况发生时,会提示超时错误。我想让它在发生这样的错误时,程序继续运行而不是完全停止。

代码是:

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。

4

2 回答 2

1

即使在page_load_timeout错误的情况下,要遍历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)用于重现页面加载超时,仅用于演示目的。
    • url列表仅用于演示目的。

您可以在How to set the timeout of 'driver.get' for python selenium 3.8.0?中找到有关页面加载超时的详细讨论?

于 2019-03-29T21:57:46.227 回答
0

您可以使用 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)
于 2019-03-29T21:54:23.353 回答