Python_expected_conditions模組用法簡介
阿新 • • 發佈:2021-10-04
轉載來源:https://www.cnblogs.com/dream66/p/12665563.html
一、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')
金鱗豈是池中物,一遇風雲便化龍。