1. 程式人生 > >Python+Selenium筆記(十):元素等待機制

Python+Selenium筆記(十):元素等待機制

tor 好的 菜單 自己 定時 場景 ive add nal

(一) 前言

突然的資源受限或網絡延遲,可能導致找不到目標元素,這時測試報告會顯示測試失敗。這時需要一種延時機制,來使腳本的運行速度與程序的響應速度相匹配,WebDriver為這種情況提供了隱式等待和顯式等待兩種機制。

(二) 隱式等待

一旦設置隱式等待時間,就會作用於這個WebDriver實例的整個生命周期(對所有的元素查找都生效),設置隱式等待時間後,Webdriver會在一定時間內持續檢測和搜尋DOM,以便於查找一個或多個不是立即加載成功並可用的元素。隱式等待的默認時間是0. WebDriver使用implicitly_wait()來設置等待時間,單位秒。超過等待時間還沒找到,就報NoSuchElementException異常。

#設置超時時間為10秒

driver.implicitly_wait(10)

(三) 顯式等待

WebDriver提供了WebDriverWait類和expected_conditions模塊來實現顯式等待。相比隱式等待,顯示等待更加智能。顯示等待就是設置一個前置條件,在等待時間內,每隔一段時間檢查一次前置條件是否滿足,滿足則執行下一步,超時則報TimeoutException異常。

(四) WebDriverWait

WebDriverWait(driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None
driver:瀏覽器驅動實例
timeout:等待時間,單位秒
poll_frequency:每隔多長時間檢查一次,默認0.5秒
ignored_exceptions:忽略的異常,默認只有NoSuchElementException
until方法 和 until_not方法:
until(method, message=‘‘):method指要執行的方法(等待時間內每隔一段時間,執行一次),直到返回值為true,超時則報TimeoutException異常,message將傳入異常(message參數可不填)

until_not(method, message=‘‘):直到返回值為false,其他和until相同

(五)

expected_conditions模塊

expected_conditions模塊提供了多種定義好的前置條件,需要配合WebDriverWait使用。

預期等待條件(前置條件)

簡單說明

element_to_be_clickable(locator)

參數:locator,指一組(By,locator)

例如:WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable((By.NAME,‘11‘)))
下面的都是以這種方式,只是前置條件不同,傳的參數也可能不同
WebDriverWait(driver,10).until()
等待查找的元素可見並且可用,以便可以點擊,返回定位到的元素

element_to_be_selected(locator)
等待直到元素被選中
invisibility_of_element_located(locator)

等待一個元素在DOM中不可見 或不存在

presence_of_all_elements_located(locator)

等待至少有一個定位器查找的元素出現在網頁中,返回一組元素

presence_of_element_located(locator)

等待定位器查找的元素出現在網頁中,或者可以在DOM中找到,返回一個被定位到的元素

text_to_be_present_in_element(locator,text)
參數:text,指定的文本
等待元素能被定位,並且帶有指定的文本信息
 
title_contains(title)
參數:title,指要校驗標題包含的字符串
等待網頁標題包含指定的字符串,成功時返回True,否則返回false
 
title_is(title)
參數:title,指要校驗的標題
等待網頁標題與預期一致,成功時返回True,否則返回false
 
visibility_of(element)
參數:element,指一個元素
 
等待元素出現在DOM中,是可見的,並且寬和高都大於0,變為可見的,將返回一個元素(同一個)
 
visibility_of_element_located(locator)
 
等待元素出現在DOM中,是可見的,並且寬和高都大於0,變為可見的,將返回一個元素
 
alert_is_present()
 
判斷是否存在警告窗口

(六) expected_conditions 示例

下面的代碼,try: 部分,每一部分都是獨立可用的(我只是驗證不同前置條件的用法後就註釋掉)。另外這裏只對方法的使用方式(方法的功能)進行說明,不對使用場景進行說明(比如有沒有必要這麽做什麽的)。

 1 from selenium import webdriver
 2 from selenium.webdriver.support.ui import WebDriverWait
 3 from selenium.webdriver.support import expected_conditions
 4 from selenium.webdriver.common.by import By
 5 
 6 driver = webdriver.Firefox()
 7 driver.maximize_window()
 8 driver.get(https://www.cnblogs.com/)
 9 
10 # try:
11 #     #等待博客園首頁的【找找看】按鈕可見並可用
12 #     search_btn = WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable((By.CLASS_NAME,‘search_btn‘)))
13 #     print(search_btn.get_attribute(‘value‘))
14 
15 # try:
16 #     login_area = driver.find_element_by_css_selector(‘#login_area‘)
17 #     login = login_area.find_element_by_link_text(‘登錄‘)
18 #     login.click()
19 #     remember_me = driver.find_element_by_id(‘remember_me‘)
20 #     remember_me.click()
21 #     #等待直到登錄頁面的復選框被選中
22 #     WebDriverWait(driver, 10).until(expected_conditions.element_located_to_be_selected((By.ID, ‘remember_me‘)))
23 
24 # try:
25 #     search_file = driver.find_element_by_id(‘zzk_q‘)
26 #     search_btn = driver.find_element_by_class_name(‘search_btn‘)
27 #     search_file.send_keys(‘python‘)
28 #     search_btn.click()
29 #     #網頁標題是否包含 python
30 #     WebDriverWait(driver, 10).until(expected_conditions.title_contains(‘python‘))
31 
32 try:
33     search_file = driver.find_element_by_id(zzk_q)
34     #檢查元素是否出現在DOM中,是可見的,並且寬和高都大於0
35     search_file = WebDriverWait(driver,10).until(expected_conditions.visibility_of(search_file))
36     print(search_file)
37 finally:
38     driver.quit()

(七) 示例(自定義前置條件)

expected_conditions類提供了多種定義好的前置條件(預期等待條件),沒有前置條件符合時,也可以通過WebDriverWait自定義前置條件。

下面這個是WebDriverWait類自帶的部分註釋。

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        """
           Example:
            from selenium.webdriver.support.ui import WebDriverWait \n
            element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
                        until_not(lambda x: x.find_element_by_id("someId").is_displayed())
        """

示例:(等待博客園個人主頁(點擊首頁的園子跳轉到的頁面)的下拉菜單有5個可選項)

#lambda表達式其實就是一個匿名函數,冒號左邊的可以理解為函數名及參數,右邊的可以理解為函數的返回值,具體可以百度python lambda

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

profile = webdriver.FirefoxProfile    (rC:\Users\quanhua\AppData\Roaming\Mozilla\Firefox\Profiles\tnwjkr4m.selenium)
driver = webdriver.Firefox(profile)
driver.maximize_window()
driver.get(https://home.cnblogs.com/)
try:
    #等待 博客園個人主頁中的下拉菜單有5個可選項
    WebDriverWait(driver,10).until(lambda l:len(Select(l.find_element_by_id(sel_application)).options) == 5)
finally:
    driver.quit()

(八) 總結

應用元素等待機制,對於構建高度穩定可靠的測試是必不可少的。在使用過程中,應該盡量避免隱式等待和顯示等待混合使用。至於隱式等待和顯示等待的優缺點,看書上和網上一般是比較推薦使用顯示等待,不過我自己試了下,暫時是沒看出在運行速度方面有多大區別(可能等以後有比較豐富的項目經驗後,再回頭來說說隱式等待和顯示等待的優缺點)。

Python+Selenium筆記(十):元素等待機制