好用的expected_conditions模組

新夢想IT發表於2022-10-20




前言

行動是成功的階梯,行動越多,登得越高。

不能懶,繼續多更新分享一些給大家~~ 

一、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(')


# 等待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(')

#等待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(')

        #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('))

#判斷當前頁面的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')


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69940641/viewspace-2919425/,如需轉載,請註明出處,否則將追究法律責任。

相關文章