【01】selenium之WebDriverWait類(等待機制)


 在自動化測試腳本的運行過程中,可以通過設置等待的方式來避免由於網絡延遲或瀏覽器卡頓導致的偶然失敗,常用的等待方式有三種:

一、固定等待(time)

  固定待是利用python語言自帶的time庫中的sleep()方法,固定等待幾秒。這種方式會導致這個腳本運行時間過長,不到萬不得已盡可能少用。(注:腳本調試過程時,還是可以使用的,方便快捷)

from selenium import webdriver import time #驅動瀏覽器
driver = webdriver.Chrome() #設置窗口最大化
driver.maximize_window() driver.get('https://www.baidu.com/') #設置固定等待
time.sleep(2) driver.quit()

 二、隱式等待(implicitly_wait())

 webdriver類提供了implicitly_wait()方法來配置超時時間。隱式等待表示在自動化實施過程中,為查找頁面元素或者執行命令設置一個最長等待時間。如果在規定時間內頁面元素被找到或者命令被執行完成,則執行下一步,否則繼續等待直到設置的最長等待時間截止

from selenium import webdriver #驅動瀏覽器
driver = webdriver.Chrome() #設置隱式等待
driver.implicitly_wait(30) #設置窗口最大化
driver.maximize_window() driver.get('https://www.baidu.com/')

   注:隱式等待的好處是不用像固定等待方法一樣死等時間N秒,可以在一定程度上提升測試用例的執行效率。不過這種方法也存在一定的弊端,那就是程序會一直等待整個頁面加載完成,也就是說瀏覽器窗口標簽欄中不再出現轉動的小圓圈,才會繼續執行下一步。

三、顯式等待(WebDriverWait)

  顯示等待會每個一段時間(該時間一般都很短,默認為0.5秒,也可以自定義),執行自定義的程序判斷條件,如果判斷條件成立,就執行下一步,否則繼續等待,直到超過設定的最長等待時間,然后拋出TimeOutEcpection的異常信息。

  • alert_is_present():判斷頁面是否出現alert框
# coding:utf-8
from selenium import webdriver #導入By類
from selenium.webdriver.common.by import By #導入顯示等待類
from selenium.webdriver.support.ui import WebDriverWait #導入期望場景類
from selenium.webdriver.support import expected_conditions driver = webdriver.Chrome() #alert_is_present():判斷頁面是否出現alert框
result=WebDriverWait(driver,10).until(expected_conditions.alert_is_present()) print(result.text)
  • element_located_selection_state_to_be(locator,state):判斷一個元素的狀態是否是給定的選擇狀態

    第一個傳入參數是一個定位器,定位器是一個元組(by,path);第二個傳入參數表示期望的元素狀態,True表示選中狀態,Flase表示未選中

#element_located_selection_state_to_be():判斷一個元素的狀態是否是給定的選擇狀態
result=WebDriverWait(driver,10).until(expected_conditions.element_located_selection_state_to_be((By.ID,'kw'),True))
  • element_selection_state_to_be(driverObject,state):判斷給定的元素是否被選中

   第一個傳入參數是一個webdriver對象,第二個參數是期望的元素的狀態,True表示選中狀態,Flase表示未選中

#element_selection_state_to_be():判斷給定的元素是否被選中
result=WebDriverWait(driver,10).until(expected_conditions.element_selection_state_to_be((driver.find_element_by_id('kw')),True))
  • element_located_to_be_selected(locator):期望某個元素處於被選中狀態

   參數傳入的是一個定位器

#element_located_to_be_selected():期望某個元素處於被選中狀態
result=WebDriverWait(driver,10).until(expected_conditions.element_located_to_be_selected((By.ID,'kw')))
  • element_to_be_selected():期望某個元素處於選中狀態

   傳入參數是一個webdriver實例對象

#element_to_be_selected():期望某個元素處於選中狀態
result=WebDriverWait(driver,10).until(expected_conditions.element_to_be_selected(driver.find_element_by_id('kw')))
  • element_to_be_clickable():判斷元素是否可見並且能被單擊,條件滿足返回頁面元素對象,否則返回Flase
#element_to_be_clickable():判斷元素是否可見並且能被單擊,條件滿足返回頁面元素對象,否則返回Flase
result=WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable(driver.find_element_by_id('hh')))
  • frame_to_be_available_and_switch_to_it(parm):判斷frame是否可用

   如果可用返回True並切入到該frame,參數parm可以是定位器locator(by,path)組成的元組,或者定位方式:id、name、index(frame在頁面上索引號),或者webelement對象。

#frame_to_be_available_and_switch_to_it():判斷frame是否可用 #傳入ID值‘kk’
result1=WebDriverWait(driver,10,0.2).until(EC.frame_to_be_available_and_switch_to_it(By.ID,'kw')) #傳入frame的webelement對象
result2=WebDriverWait(driver,10,0.2).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_id('kw'))) #傳入frame在頁面中索引號
result3=WebDriverWait(driver,10,0.2).until(EC.frame_to_be_available_and_switch_to_it(1))
  • invisibility_of_element_located(locator):希望某個元素不可見或者不存在DOM中

   滿足條件返回True,否則返回定位到的元素對象

#invisibility_of_element_located():希望某個元素不可見或者不存在DOM中,滿足條件返回True,否則返回定位到的元素對象
result8=WebDriverWait(driver,10,0.2).until(EC.invisibility_of_element_located(By.ID,'kw'))
  • visibility_of_element_located(locator):希望某個元素出現在DOM中並且可見
  滿足條件返回該元素的頁面元素對象
#visibility_of_element_located():希望某個元素出現在DOM中並且可見,滿足條件返回該元素的頁面元素對象
result9=WebDriverWait(driver,10,0.2).until(EC.visibility_of_element_located(driver.find_element_by_id('kw')))
  • visibility_of(webelement):希望某個元素出現在頁面的DOM中,並且可見,滿足條件返回該元素的頁面元素對象
#visibility_of():希望某個元素出現在頁面的DOM中,並且可見,滿足條件返回該元素的頁面元素對象
result10=WebDriverWait(driver,10,0.2).until(EC.visibility_of(driver.find_element_by_id('kw'))
  • visibility_of_any_elements_located(locator):希望某個元素出現在DOM中並且可見

   如果滿足條件返回該元素的頁面元素對象

#visibility_of_any_elements_located():希望某個元素出現在DOM中並且可見
result11=WebDriverWait(driver,10,0.2).until(EC.visibility_of(By.TAG_NAME,'input'))
  • presence_of_all_elements_located(locator):判斷頁面至少有一個如果元素出現,如果滿足條件,返回所有滿足定位表達式的頁面元素
#presence_of_all_elements_located():判斷頁面至少有一個如果元素出現,如果滿足條件,返回所有滿足定位表達式的壓面元素
result12=WebDriverWait(driver,10,0.2).until(EC.presence_of_all_elements_located(By.ID,'kw'))
  • presence_of_element_located(locator):判斷某個元素是否存在DOM中,不一定可見,存在返回該元素對象
#presence_of_element_located():判斷某個元素是否存在DOM中,不一定可見,存在返回該元素對象
result12=WebDriverWait(driver,10,0.2).until(EC.presence_of_element_located(By.ID,'kw'))
  • staleness_of(webelement):判斷一個元素是否仍在DOM中,如果在規定時間內已經移除返回True,否則返回Flase
#staleness_of():判斷一個元素是否仍在DOM中,如果在規定時間內已經移除返回True,否則返回Flase
result13=WebDriverWait(driver,10,0.2).until(EC.staleness_of(driver.find_element_by_id('kw')))
  • text_to_be_present_in_element():判斷文本內容test是否出現在某個元素中,判斷的是元素的text
#text_to_be_present_in_element():判斷文本內容test是否出現在某個元素中,判斷的是元素的text
result15=WebDriverWait(driver,10,0.2).until(EC.text_to_be_present_in_element(By.TAG_NAME,"p"))
  • text_to_be_present_in_element_value():判斷text是否出現在元素的value屬性值中
#text_to_be_present_in_element_value():判斷text是否出現在元素的value屬性值中
result16=WebDriverWait(driver,10,0.2).until(EC.text_to_be_present_in_element_value((By.ID,'kw'),'隨便'))
  • title_contains():判斷頁面title標簽的內容包含partial_title,只需要部分匹配即可
#title_contains():判斷頁面title標簽的內容包含partial_title,只需要部分匹配即可,包含返回True,不包含返回Flase
result17=WebDriverWait(driver,10,0.2).until(EC.title_contains("你就知道"))
  • title_is():判斷頁面title內容是與傳入的title_text內容完全匹配
#title_is():判斷頁面title內容是與傳入的title_text內容完全匹配,匹配返回True,否則返回Flase
result18=WebDriverWait(driver,10,0.2).until(EC.title_is("百度一下,你就知道"))

 


免責聲明!

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



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