selenium細節實戰02-->好用的expected_conditions模塊


前言

        行動是成功的階梯,行動越多,登得越高。
        不能懶,繼續多更新分享一些給大家~~

一、expected_conditions模塊是什么?

  • 是Selenium的一個子模塊,selenium.webdriver.support.expected_conditions
  • 可以對網頁上元素是否存在,可點擊等等進行判斷,一般用於斷言或與WebDriverWait配合使用

二、expected_conditions模塊簡單應用

  • 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,等待過程中判斷網頁標題是否是"百度一下,你就知道"
# 如果是就繼續執行后續代碼,反之等待10s結束時報錯
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,等待過程中如果定位到元素,就直接執行后續的代碼,反之等待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()

實例小結:

  • 實例一與實例二中用到了顯式等待 WebDriverWait類,該塊不在此文中介紹;
  • 實例三中self.assertTrue()方法斷言括號內的表達式返回值是否為ture,在python中代表true的為 非0、非空、true,而EC.visibility_of()方法中的定位方法能定位到元素就會返回一個對象,滿足非空為true,所以斷言會通過;
  • 注意EC.visibility_of()方法返回的對象非真實元素對象,所以不能執行如下代碼:(正確方式參照實例二的寫法)
element = EC.visibility_of(self.driver.find_element(By.ID,'kw'))
element.send_keys('newdream')

三、expected_conditions模塊用法匯總(不定期更新)

#判斷當前頁面的title是否精確等於預期,返回布爾值
WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
#判斷當前頁面的title是否包含預期字符串,返回布爾值
WebDriverWait(driver,10).until(EC.title_contains('new'))
#判斷當前頁面的url是否精確等於預期,返回布爾值
WebDriverWait(driver,10).until(EC.url_contains('https://www.baidu.com'))
#判斷當前頁面的url是否包含預期字符串,返回布爾值
WebDriverWait(driver,10).until(EC.url_contains('baidu'))
#判斷當前頁面的url是否滿足字符串正則表達式匹配,返回布爾值
WebDriverWait(driver,10).until(EC.url_matches('.+baidu.+'))
#判斷元素是否出現,只要有一個元素出現,返回元素對象
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進去,如果可以的話,返回True並且switch進去
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的內容
WebDriverWait(driver,10).until(EC.alert_is_present())
#還有不少,用到了繼續更新......

備注:以上整理大家要注意參數和返回值,部分參數是元素對象,部分是locator的元組,如(By.NAME,'tj_trnews')


免責聲明!

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



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