Python & Selenium--handling the problem of long page load times

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.