selenium常用的API(四)設置get方法最大加載時間


我們在進行自動化測試的時候,使用get方法打開頁面時會等到頁面完全加載完才會執行后續操作,

有時我們需要的元素已加載完成,而部分JS未加載完導致加載時間很長,這無疑增加了自動化測試的時間,

針對此情況,可使用set_page_load_timeout(seconds)方法設置超時時間,然后捕獲超時異常,然后繼續執行后續操作。

#encoding=utf-8
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import unittest

class VisitUrl(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")

    def test_visitURL(self):
        visitURL = "http://www.google.com"
        #限定頁面加載時間最大為3秒
        self.driver.set_page_load_timeout(3)
        try:
            self.driver.get(visitURL)
        except TimeoutException:
            print u'頁面加載超時!'
             #當頁面加載時間超過設定時間,通過執行Javascript來停止載,然后繼續執行后續操作
            self.driver.execute_script('window.stop()')
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
unittest.main()

 


免責聲明!

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



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