1. 程式人生 > >python裏三種等待元素的方法

python裏三種等待元素的方法

python元素等待 python顯性等待 wait方法 python隱性等待

在做web或app的自動化測試經過會出現找不到元素而報錯的情況,很多時候是因為元素

還沒有被加載出來,查找的代碼就已經被執行了,自然就找不到元素了。那麽我可以用等待

元素加載完成後再執行查找元素的code。

Python裏有三種等待的方式:
一、 強制等待

Sleep(54)
這個方法在time模塊,使用時通過from time import sleep導入
比如:

Sleep(10) #表示強行等待10s再執行下一句代碼
Driver.find_element_by_xpath(“xxxxxx”)

這種等待方式時間到了就執行下個語句,但比較死板,不能保證在等待的時間內元素真正被加載了出來。

二、 隱性等待

Implicitly_wait(xxx)
這個等待表示在規定的時間內頁面的所有元素都加載完了就執行下一步,否則一直等到時間截止,然後再繼續下一步。

Driver=webdriver.Chrome()
Driver.implicitly_wait(10)#等待10s
Driver.get(“XXX”)

這個方法的缺點是你需要的元素已經加載出來了,但頁面還沒有加載完,再需要繼續等待頁面加載完才能執行下一步操作。
看看第三種方法,比較靈活

三、 顯性等待

WebDriverWait,配合該類的until()和until_not()方法,表示程序每隔x秒去判斷一下指

定的元素是否加載完,加載完了就執行下一步,否則繼續每隔x秒去判斷,指定時間截

止。如果超時就會拋出異常。

from selenium import webdriver  
from selenium.webdriver.support.wait import WebDriverWait  
from selenium.webdriver.support import expected_conditions as EC  
from selenium.webdriver.common.by import By

locator=(By.XPATH,”xxxxxxx”)
d = webdriver.Chorme()
d.get(“http://www.sina.com”)
WebDriverWait(d,10,1).unitl(EC.presence_of_element_located(locator))
Print(“”XXX“”)

這裏表示等待10s,每隔1s去檢查一次元素是否出現,出現了就執行下一步,直到10s

結束後還沒有出現就會拋出異常。

實例講解:(用了yuuwee的博客內容: https://www.cnblogs.com/yuuwee/p/6635652.html)

WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道"))
‘‘‘判斷title,返回布爾值‘‘‘

WebDriverWait(driver,10).until(EC.title_contains(u"百度一下"))
‘‘‘判斷title,返回布爾值‘‘‘

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,‘kw‘)))
‘‘‘判斷某個元素是否被加到了dom樹裏,並不代表該元素一定可見,如果定位到就
返回WebElement‘‘‘

WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,‘su‘)))
‘‘‘判斷某個元素是否被添加到了dom裏並且可見,可見代表元素可顯示且寬和高都大
於0‘‘‘

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value=‘kw‘)))
‘‘‘判斷元素是否可見,如果可見就返回這個元素‘‘‘

WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,
‘.mnav‘)))
‘‘‘判斷是否至少有1個元素存在於dom樹中,如果定位到就返回列表‘‘‘

WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR
,‘.mnav‘)))
‘‘‘判斷是否至少有一個元素在頁面中可見,如果定位到就返回列表‘‘‘

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id=‘
u1‘]/a[8]"),u‘設置‘))
‘‘‘判斷指定的元素中是否包含了預期的字符串,返回布爾值‘‘‘

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECT
OR,‘#su‘),u‘百度一下‘))
‘‘‘判斷指定元素的屬性值中是否包含了預期的字符串,返回布爾值‘‘‘

#WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
‘‘‘判斷該frame是否可以switch進去,如果可以的話,返回True並且switch進去,否
則返回False‘‘‘
#註意這裏並沒有一個frame可以切換進去

WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,‘#s
wfEveryCookieWrap‘)))
‘‘‘判斷某個元素在是否存在於dom或不可見,如果可見返回False,不可見返回這個元素‘‘‘
#註意#swfEveryCookieWrap在此頁面中是一個隱藏的元素

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘u1‘]/a[
8]"))).click()
‘‘‘判斷某個元素中是否可見並且是enable的,代表可點擊‘‘‘
driver.find_element_by_xpath("//*[@id=‘wrapper‘]/div[6]/a[1]").click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘wrap
per‘]/div[6]/a[1]"))).click()

#WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,‘su‘)))
‘‘‘等待某個元素從dom樹中移除‘‘‘
#這裏沒有找到合適的例子

WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPAT
H,"//*[@id=‘nr‘]/option[1]")))
‘‘‘判斷某個元素是否被選中了,一般用在下拉列表‘‘‘

WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.
XPATH,"//*[@id=‘nr‘]/option[1]"),True))
‘‘‘判斷某個元素的選中狀態是否符合預期‘‘‘

WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//
*[@id=‘nr‘]/option[1]"),True))
‘‘‘判斷某個元素的選中狀態是否符合預期‘‘‘
driver.find_element_by_xpath(".//*[@id=‘gxszButton‘]/a[1]").click()

instance = WebDriverWait(driver,10).until(EC.alert_is_present())
‘‘‘判斷頁面上是否存在alert,如果有就切換到alert並返回alert的內容‘‘‘
print instance.text
instance.accept()

driver.close()

python裏三種等待元素的方法