2016-10-11 128 views

回答

1

設置set_page_load_timeout,則處理異常:

from __future__ import print_function 

from selenium import webdriver 
from selenium.common.exceptions import TimeoutException 

def get_page(driver, page): 
    try: 
     driver.get(page) 
    except TimeoutException: 
     pass 


def main(): 

    driver = webdriver.Chrome() 

    try: 
     driver.set_page_load_timeout(5) 
     get_page(driver, "http://your/long/url") 

     print("Returned from page get") 

     from pprint import pprint as pp 
     pp(driver.page_source) 

    finally: 
     driver.quit() 


if __name__ == "__main__": 
    main()