您好,登錄后才能下訂單哦!
小編給大家分享一下Expected conditions模塊是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
一、expected_conditions模塊是什么?
是Selenium的一個子模塊,selenium.webdriver.support.expected_conditions
可以對網(wǎng)頁上元素是否存在,可點擊等等進(jìn)行判斷,一般用于斷言或與WebDriverWait配合使用
二、expected_conditions模塊簡單應(yīng)用
2.1 WebDriverWait與expected_conditions配合使用實例一
import os import time from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get('https://www.baidu.com') # 等待10s,等待過程中判斷網(wǎng)頁標(biāo)題是否是"百度一下,你就知道" # 如果是就繼續(xù)執(zhí)行后續(xù)代碼,反之等待10s結(jié)束時報錯 WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
2.2 WebDriverWait與expected_conditions配合使用實例二
from selenium import webdriver 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.get('https://www.baidu.com') #等待10s,等待過程中如果定位到元素,就直接執(zhí)行后續(xù)的代碼,反之等待10s后報錯誤信息 element = WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw'))) element.send_keys( '新夢想軟件測試' )
2.3 unittest與expected_conditions配合使用實例
import time import unittest from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC class TestDemo(unittest.TestCase): def setUp(self) : self.driver = webdriver.Chrome() def tearDown(self): time.sleep(2) self.driver.quit() def test_searchinputbox_is_visibility(self): self.driver.get('https://www.baidu.com') #EC.visibility_of()判斷元素是否可見,如果可見就返回這個元素 self.assertTrue(EC.visibility_of(self.driver.find_element(By.ID,'kw'))) if __name__=='__main__': unittest.main()
實例小結(jié):
實例一與實例二中用到了顯式等待 WebDriverWait類,該塊不在此文中介紹;
實例三中self.assertTrue()方法斷言括號內(nèi)的表達(dá)式返回值是否為ture,在python中代表true的為 非0、非空、true,而
EC.visibility_of()方法中的定位方法能定位到元素就會返回一個對象,滿足非空為true,所以斷言會通過;
注意EC.visibility_of()方法返回的對象非真實元素對象,所以不能執(zhí)行如下代碼:(正確方式參照實例二的寫法)
element = EC.visibility_of(self.driver.find_element(By.ID,'kw'))
element.send_keys('newdream')
三、expected_conditions模塊用法匯總
#判斷當(dāng)前頁面的title是否精確等于預(yù)期,返回布爾值 WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道")) #判斷當(dāng)前頁面的title是否包含預(yù)期字符串,返回布爾值 WebDriverWait(driver,10).until(EC.title_contains('new')) #判斷當(dāng)前頁面的url是否精確等于預(yù)期,返回布爾值 WebDriverWait(driver,10).until(EC.url_contains('https://www.baidu.com')) #判斷當(dāng)前頁面的url是否包含預(yù)期字符串,返回布爾值 WebDriverWait(driver,10).until(EC.url_contains('baidu')) #判斷當(dāng)前頁面的url是否滿足字符串正則表達(dá)式匹配,返回布爾值 WebDriverWait(driver,10).until(EC.url_matches('.+baidu.+')) #判斷元素是否出現(xiàn),只要有一個元素出現(xiàn),返回元素對象 WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw'))) #判斷元素是否可見,返回元素對象 WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw'))) #判斷元素是否包含指定文本,返回布爾值 WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.NAME,'tj_trnews'),'新聞')) #判斷該frame是否可以switch進(jìn)去,如果可以的話,返回True并且switch進(jìn)去 WebDriverWait(driver,10,).until(EC.frame_to_be_available_and_switch_to_it(By.xpath,'//iframe')) #判斷某個元素是否可見并且是可點擊的,如果是的就返回這個元素,否則返回False WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,'tj_trnews'))) #判斷某個元素是否被選中,一般用在下拉列表 WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.xpath,'//input[@type="checkbox"]'))) #判斷頁面上是否存在alert,如果有就切換到alert并返回alert的內(nèi)容 WebDriverWait(driver,10).until(EC.alert_is_present())
備注:以上整理大家要注意參數(shù)和返回值,部分參數(shù)是元素對象,部分是locator的元組,如(By.NAME,'tj_trnews')
看完了這篇文章,相信你對Expected conditions模塊是什么有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。