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,等待過程中判斷網頁標題是否是"百度一下,你就知道"
# 如果是就繼續(xù)執(zhí)行后續(xù)代碼,反之等待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,等待過程中如果定位到元素,就直接執(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()
實例小結:
實例一與實例二中用到了顯式等待 WebDriverWait類,該塊不在此文中介紹;
實例三中self.assertTrue()方法斷言括號內的表達式返回值是否為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模塊用法匯總
#判斷當前頁面的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')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY
- Vue項目報錯:Uncaught SyntaxError: Unexpected token <
- vue-cli 打包后提交到線上出現 "Uncaught SyntaxError:Unexpected token" 報錯
- python出現"IndentationError: unexpected indent"錯誤解決辦法
- shell腳本報錯:"[: =: unary operator expected"解決辦法
- JS提示:Uncaught SyntaxError: Unexpected token ILLEGAL錯誤的解決方法
- JS提示:Uncaught SyntaxError:Unexpected token ) 錯誤的解決方法
- PHP錯誤Parse error: syntax error, unexpected end of file in test.php on line 12解決方法
相關文章
Python實現SSH遠程登陸,并執(zhí)行命令的方法(分享)
下面小編就為大家?guī)硪黄狿ython實現SSH遠程登陸,并執(zhí)行命令的方法(分享)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
解決Python內層for循環(huán)如何break出外層的循環(huán)的問題
今天小編就為大家分享一篇解決Python內層for循環(huán)如何break出外層的循環(huán)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
PyCharm配置anaconda環(huán)境的步驟詳解
PyCharm是一款很好用很流行的python編輯器。Anaconda通過管理工具包、開發(fā)環(huán)境、Python版本,大大簡化了你的工作流程。今天通過本文給大家分享PyCharm配置anaconda環(huán)境,感興趣的朋友一起看看吧2020-07-07
Python中os.path.join函數的用法舉例詳細講解
這篇文章主要介紹了Python中os.path.join函數用法的相關資料,os.path.join函數用于拼接路徑,根據操作系統(tǒng)自動選擇分隔符,它可以處理不同參數的情況,包括絕對路徑、空字符串和以特定字符開始的參數,需要的朋友可以參考下2025-02-02
django-rest-swagger對API接口注釋的方法
今天小編就為大家分享一篇django-rest-swagger對API接口注釋的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

