selenium WebDriverWait


Selenium WebDriverWait的知識:

一、webdrivewait 示例代碼

 

  1.  from selenium import webdriver
  2.   from selenium.webdriver.support.wait import WebDriverWait
  3.   from selenium.webdriver.support import expected_conditions as EC
  4.   from selenium.webdriver.common.by import By
  5.   import time 
  6.  driver = webdriver.Chrome()
  7.  driver.get( "http://www.baidu.com/")
  8.  locator = (By.ID, "kw") 
  9.   try:
  10. ele = WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
  11.  driver.find_element_by_id( "kw").send_keys('abc')
  12.  time.sleep( 1) #為了看效果
  13.   except:
  14.  print( "ele can't find")
  15.   finally:
  16.  driver.quit()

接下來一點點分析下這個語句

WebDriverWait(driver,10).until(EC.presence_of_element_located(locator))

(1)WebDriverWait 方法

 

  1.  driver: 傳入WebDriver實例,即我們上例中的driver
  2.  timeout: 超時時間,等待的最長時間(同時要考慮隱性等待時間)
  3.  poll_frequency: 調用 until或until_not中的方法的間隔時間,默認是0.5秒
  4.  ignored_exceptions: 忽略的異常,如果在調用 until或until_not的過程中拋出這個元組中的異常,
  5.  則不中斷代碼,繼續等待,如果拋出的是這個元組外的異常,則中斷代碼,拋出異常。默認只有NoSuchElementException。

 

(2)WebDriverWait 方法后面有兩種等待方式

until

  1.  method: 在等待期間,每隔一段時間(__init__中的poll_frequency)調用這個傳入的方法,直到返回值不是False
  2.   message: 如果超時,拋出TimeoutException,將message傳入異常

until_not

  1.  與 until相反,until是當某元素出現或什么條件成立則繼續執行,
  2.  until_not是當某元素消失或什么條件不成立則繼續執行,參數也相同,不再贅述。

看了以上內容基本上很清楚了,調用方法如下:

WebDriverWait(driver, 超時時長, 調用頻率, 忽略異常).until(可執行方法, 超時時返回的信息)

這里需要特別注意的是until或until_not中的可執行方法method參數,很多人傳入了WebElement對象,如下:

WebDriverWait(driver, 10).until(driver.find_element_by_id('kw')) # 錯誤

這是錯誤的用法,這里的參數一定要是可以調用的,即這個對象一定有 __call__() 方法,否則會拋出異常:

TypeError: 'xxx' object is not callable

在這里,你可以用selenium提供的 expected_conditions 模塊中的各種條件,也可以用WebElement的 is_displayed() 、is_enabled()is_selected() 方法,或者用自己封裝的方法都可以,那么接下來我們看一下selenium提供的條件有哪些:

expected_conditions

expected_conditions是selenium的一個模塊,其中包含一系列可用於判斷的條件:

selenium.webdriver.support.expected_conditions(模塊)

以下兩個條件類驗證title,驗證傳入的參數title是否等於或包含於driver.title 
title_is 
title_contains

以下兩個條件驗證元素是否出現,傳入的參數都是元組類型的locator,如(By.ID, ‘kw’) 
顧名思義,一個只要一個符合條件的元素加載出來就通過;另一個必須所有符合條件的元素都加載出來才行 
presence_of_element_located 
presence_of_all_elements_located

以下三個條件驗證元素是否可見,前兩個傳入參數是元組類型的locator,第三個傳入WebElement 
第一個和第三個其實質是一樣的 
visibility_of_element_located 
invisibility_of_element_located 
visibility_of

以下兩個條件判斷某段文本是否出現在某元素中,一個判斷元素的text,一個判斷元素的value 
text_to_be_present_in_element 
text_to_be_present_in_element_value

以下條件判斷frame是否可切入,可傳入locator元組或者直接傳入定位方式:id、name、index或WebElement 
frame_to_be_available_and_switch_to_it

以下條件判斷是否有alert出現 
alert_is_present

以下條件判斷元素是否可點擊,傳入locator 
element_to_be_clickable

以下四個條件判斷元素是否被選中,第一個條件傳入WebElement對象,第二個傳入locator元組 
第三個傳入WebElement對象以及狀態,相等返回True,否則返回False 
第四個傳入locator以及狀態,相等返回True,否則返回False 
element_to_be_selected 
element_located_to_be_selected 
element_selection_state_to_be 
element_located_selection_state_to_be

最后一個條件判斷一個元素是否仍在DOM中,傳入WebElement對象,可以判斷頁面是否刷新了 
staleness_of

確認新窗口打開

new_window_is_opened

上面是所有17個condition,與until、until_not組合能夠實現很多判斷,如果能自己靈活封裝,將會大大提高腳本的穩定性

 原文鏈接:https://blog.csdn.net/huilan_same/article/details/52544521

                   https://www.cnblogs.com/linbao/p/8081598.html


免責聲明!

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



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