Selenium 3種等待方式

加入等待時間,主要是考慮到網頁加載須要時間,可能因爲網速慢,或者使用了 ajax 技術實現了異步加載等,若是程序找不到指定的頁面元素,就會致使報錯發生。python

經常使用的有3種等待方式:web

  1. 強制等待
  2. 隱式等待
  3. 顯示等待

強制等待

使用 Python 自身的庫 time.sleep() 能夠實現強制等待。ajax

強制等待使用簡單,可是,當網絡條件良好的時候,建議減小使用,由於若是頻繁使用強制等待的方式等待元素加載,會致使整個項目的自動化時間延長。瀏覽器

這種等待方式的使用場景主要是腳本調試網絡

隱式等待

隱式等待其實是,設置了一個最長的等待時間,若是在這段時間內可以定位到目標,則執行下一步操做,不然會一致等到規定時間結束,而後再執行下一步。dom

隱式等待設置一次,對整個 driver 週期都可以起做用,因此,在最開始設置一次便可異步

注意:在同一個 driver 週期中遇到強制等待,可能會致使隱式等待失效
# 隱式等待,京東的「新人福利」
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()  # 打開瀏覽器
driver.maximize_window()  # 瀏覽器最大化
driver.get("https://www.jd.com/")  # 跳轉至京東
driver.implicitly_wait(10)  # 隱式等待 10s
element = driver.find_element_by_xpath("//*[@class='user_profit_lk']")  # 定位元素
element.click()  # 點擊
sleep(3)

driver.quit()  # 關閉瀏覽器

顯式等待

WebDriverWait 是 Selenium 提供的顯式等待的模塊,使用原理是:在指定的時間範圍內,等待到符合/不符合某個條件爲止。測試

導入方式:ui

from selenium.webdriver.support.wait import WebDriverWait

WebDriverWait 參數:spa

序號 參數 描述
1 driver 傳入的 WebDriverWait 實例
2 timeout 超時時間,等待的最長時間
3 poll_frequency 調用 until 或 until_not 中的方法的間隔時間(默認是0.5秒)
4 ignored_exceptions 忽略的異常

WebDriverWait 模塊含有兩個方法:

  1. until
  2. until_not

until 與 until_not 的參數:

序號 參數 描述
1 method 在等待期間,每隔一段時間調用這個傳入的方法,直到返回值不爲 False
2 message 若是超時,拋出 TimeoutException,將 message 傳入異常

一般狀況下,WebDriverWait 模塊會與 expected_conditions 模塊搭配使用,用來寫入 until 與 until_not 中的參數——method。

expected_conditions 模塊有如下等待條件:

序號 等待條件方法 描述
1 title_is(object) 判斷標題,是否出現
2 title_contains(object) 判斷標題,是否包含某些字符
3 presence_of_element_located(object)--經常使用 判斷某個元素,是否被加到了 dom 樹裏,並不表明該元素必定可見
4 visibility_of_element_located(object)--經常使用 判斷某個元素,是否被加到了 dom 樹裏,而且可見,寬和高都大於0
5 visibility_of(object) 判斷元素是否可見,若是可見則返回這個元素
6 presence_of_all_elements_located(object) 判斷是否至少有1個元素存在 dom 樹中
7 visibility_of_any_elements_located(object) 判斷是否至少有1個元素在頁面中可見
8 text_to_be_present_in_element(object) 判斷指定的元素中是否包含了預期的字符串
9 text_to_be_present_in_element_value(object) 判斷指定元素的屬性值是否包含了預期的字符串
10 frame_to_be_available_and_switch_to_it(object) 判斷該 frame 是否能夠 切換進去
11 invisibility_of_element_located(object) 判斷某個元素是否存在與 dom 樹中或不可見
12 element_to_be_clickable(object) 判斷某個元素中是否可見,而且是可點擊的
13 staleness_of(object) 等待某個元素從 dom 樹中刪除
14 element_to_be_selected(object) 判斷某個元素是否被選中,通常用在下拉列表中
15 element_selection_state_to_be(object) 判斷某個元素的選中狀態是否符合預期
16 element_located_selection_state_to_be(object) 判斷某個元素的選中狀態是否符合預期
17 alert_is_present(object) 判斷頁面上是否出現 alert 彈窗
# 模擬場景:點擊京東首頁的「新人福利」
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()  # 打開瀏覽器
driver.maximize_window()  # 瀏覽器最大化
driver.get("https://www.jd.com/")  # 跳轉至京東
element = WebDriverWait(driver, 20, 0.5).until(
    EC.visibility_of_element_located((By.XPATH, "//*[@class='user_profit_lk']"))
)  # 20秒內,直到元素在頁面中可定位

element.click()  # 點擊
sleep(3)

driver.quit()

顯式等待,雖然使用起來,相比其餘等待方式,顯得要複雜,可是它的優點在於靈活,經過封裝後,經過簡單的調用,就能夠運用到自動化測試項目中。

總結

相關文章
相關標籤/搜索