[轉載]Python & Selenium -- 頁面載入時間過長&啟動指定FF

來源:互聯網
上載者:User

標籤:exe   strong   bsp   pre   seconds   loading   tps   home   exception   

原文連結:https://my.oschina.net/u/2344787/blog/400507?p={{page}} 1. selenium webdriver在get方法會一直等待頁面載入完畢才會執行後面的,可如果載入時間太長會導致後續操作無法進行。

例如:get 汽車之家頁面時會等待很長時間,其實頁面基本元素都已載入,可進行後續操作

可以通過set_page_load_time()方法來設定時間,

然後捕獲timeout異常 並 通過執行Javascript來停止頁面載入 window.stop()

 1 from selenium.common.exceptions import TimeoutException 2  3 startTime = time.time() 4 print "start time is: %0.3f"%startTime 5 driver = webdriver.Firefox() 6 # 設定頁面載入限制時間 7 driver.set_page_load_timeout(30) 8 driver.maximize_window() 9 10 try:11     driver.get(‘http://www.autohome.com.cn/‘)12 except TimeoutException:  13     print ‘time out after 30 seconds when loading page‘  14     driver.execute_script(‘window.stop()‘) #當頁面載入時間超過設定時間,通過執行Javascript來stop載入,即可執行後續動作
2. 使用指定的FF來建立driver
1 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary2 myFF = FirefoxBinary(‘/root/bobo/firefox/firefox-bin‘)3 driver = webdriver.Firefox(firefox_binary=myFF)

 

[轉載]Python & Selenium -- 頁面載入時間過長&啟動指定FF

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.