使Selenium在超时错误后继续

时间:2019-03-29 21:35:06

标签: linux selenium pageload page-load-time pageloadtimeout

我有一个代码,用于返回网站列表的标题。有时,网站加载的时间很荒谬,因此,发生这种情况时,会提示超时错误。我希望这样做,以便在发生此类错误时,程序继续运行而不是完全停止。

代码是:

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。

2 个答案:

答案 0 :(得分:1)

即使在发生 page_load_timeout 错误的情况下,也要遍历URL的列表,您可以使用以下解决方案:

  • 代码块:

    try {
        // application init
        $app->init();
    } catch (Doctrine\Common\Persistence\Mapping\MappingException $e) {
        error_log("MappingException: " . $e->getTraceAsString());
        throw $e;
    }
    
  • 控制台输出:

    ini_set('log_errors_max_len', 0); // 0 - infinity length allow
    
  • 注意:

    • 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() 仅用于演示页面加载超时
    • URLs List 仅用于演示目的。
  

您可以在How to set the timeout of 'driver.get' for python selenium 3.8.0?

中找到有关页面加载超时的详细讨论。

答案 1 :(得分:0)

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

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)