Selenium Webdriver The Get method will wait until the page has finished loading before executing, but if the load time is too long it will cause subsequent operations to fail.
For example: Get Autohome page will wait for a long time, in fact, the basic elements of the page has been loaded, can be used for subsequent operations
The time can be set by the Set_page_load_time () method,
Then catch the timeout exception and stop the page load by executing JavaScript window.stop ()
From selenium.common.exceptions Import timeoutexceptionstarttime = Time.time () print "Start time is:%0.3f"% Starttimedriver = Webdriver. Firefox () driver.set_page_load_timeout () Driver.maximize_window () try:driver.get (' http://www.autohome.com.cn/') Except Timeoutexception:print ' Time out after the seconds when loading page ' Driver.execute_script (' Window.stop ( ') #当页面加载时间超过设定时间, you can perform subsequent actions by executing JavaScript to stop loading
The code in
Python & Selenium--handling the problem of long page load times