selenium學習筆記——driver.get(url) 頁面加載時間太長


# 兩個同時設置才行
# 實現效果:加載狀態停止,進行代碼下一步操作
driver.set_page_load_timeout(10)
driver.set_script_timeout(10)  # 這兩種設置都進行才有效
try:
    driver.get("https://shopee.co.id/search?keyword=jam%20tangan&page=" + str(page) + "&sortBy=" + rankBY)
except:
    driver.execute_script('window.stop()')
————————————————
版權聲明:本文為CSDN博主「cool_soup29」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/cool_soup29/article/details/88657643

 

在執行自動化測試用例過程中,發現因為網絡慢或其他原因導致driver.get(url) 時,頁面一直在加載,頁面沒有加載完成就不會去繼續執行下面的動作,但是實際上需要操作的元素已經加載出來了。

解決方法

第一步:使用 set_script_timeout() 設置等待最大時間。

第二步:到了最大等待時間后頁面如果仍然沒有加載完成,執行js代碼, driver.execute_script("window.stop()") 停止頁面加載,執行下面的自動化測試步驟。
代碼如下:

driver = self.driver
        # 設置頁面最大加載時間
        driver.set_page_load_timeout(10)
        try:
            driver.get(self.base_url)
        except TimeoutException:
            print '!!!!!!time out after 10 seconds when loading page!!!!!!'
            # 當頁面加載時間超過設定時間,通過js來stop,即可執行后續動作
            driver.execute_script("window.stop()")
  1. 設置了最大等待時間為10秒。
  2. 如果10秒沒有加載完成,打印“time out after 10 seconds when loading page!”,然后停止加載,直接執行下面的測試步驟。
    # -*- coding: utf-8 -*-
    # @Time    : 2018/8/6 11:12
    # @Author  : 銀河以北
    # @Email   : smilegks@163.com
    # @Introduction   : XXX
    
    def page_loading_timeout(driver, url, time):
        '''
        :param driver: 參數1,傳入瀏覽器對象
        :param url: 參數2,傳入url
        :param time: 參數3,設置超時時間,單位是秒
        :return:
        '''
        driver.set_page_load_timeout(time)
        try:
            driver.get(url)
        except:
            print "!!!!!!time out after %s seconds when loading page!!!!!!" % time
            # 當頁面加載時間超過設定時間,通過js來stop,即可執行后續動作
            driver.execute_script("window.stop()")

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM