[TOC]
前言
經常有小伙伴問,如何判斷一個元素是否存在,如何判斷alert彈窗出來了,如何判斷動態的元素等等一系列的判斷,在selenium的expected\_conditions模塊收集了一系列的場景判斷方法,這些方法是逢面試必考的!!!
expected\_conditions一般也簡稱EC,本篇先介紹下有哪些功能,后續更新中會單個去介紹。
<br />
## 一、功能介紹和翻譯
* title\_is: 判斷當前頁面的title是否完全等于(==)預期字符串,返回布爾值
* title\_contains : 判斷當前頁面的title是否包含預期字符串,返回布爾值
* presence\_of\_element\_located : 判斷某個元素是否被加到了dom樹里,并不代表該元素一定可見
* visibility\_of\_element\_located : 判斷某個元素是否可見. 可見代表元素非隱藏,并且元素的寬和高都不等于0
* visibility\_of : 跟上面的方法做一樣的事情,只是上面的方法要傳入locator,這個方法直接傳定位到的element就好了
* presence\_of\_all\_elements\_located : 判斷是否至少有1個元素存在于dom樹中。舉個例子,如果頁面上有n個元素的class都是'column-md-3',那么只要有1個元素存在,這個方法就返回True
* text\_to\_be\_present\_in\_element : 判斷某個元素中的text是否 包含 了預期的字符串
* text\_to\_be\_present\_in\_element\_value : 判斷某個元素中的value屬性是否 包含 了預期的字符串
* frame\_to\_be\_available\_and\_switch\_to\_it : 判斷該frame是否可以switch進去,如果可以的話,返回True并且switch進去,否則返回False
* invisibility\_of\_element\_located : 判斷某個元素中是否不存在于dom樹或不可見
* element\_to\_be\_clickable : 判斷某個元素中是否可見并且是enable的,這樣的話才叫clickable
* staleness\_of : 等某個元素從dom樹中移除,注意,這個方法也是返回True或False
* element\_to\_be\_selected : 判斷某個元素是否被選中了,一般用在下拉列表
* element\_selection\_state\_to\_be : 判斷某個元素的選中狀態是否符合預期
* element\_located\_selection\_state\_to\_be : 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入locator
* alert\_is\_present : 判斷頁面上是否存在alert
<br />
## 二、查看源碼和注釋
1. 打開python里這個目錄l可以找到:Lib\\site-packages\\selenium\\webdriver\\support\\expected\_conditions.py

```
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchFrameException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import NoAlertPresentException
"""
?* Canned "Expected Conditions" which are generally useful within webdriver
?* tests.
"""
class title_is(object):
??? """An expectation for checking the title of a page.
??? title is the expected title, which must be an exact match
??? returns True if the title matches, false otherwise."""
??? def __init__(self, title):
??????? self.title = title
??? def __call__(self, driver):
??????? return self.title == driver.title
class title_contains(object):
??? """ An expectation for checking that the title contains a case-sensitive
??? substring. title is the fragment of title expected
??? returns True when the title matches, False otherwise
??? """
??? def __init__(self, title):
??????? self.title = title
??? def __call__(self, driver):
??????? return self.title in driver.title
class presence_of_element_located(object):
??? """ An expectation for checking that an element is present on the DOM
??? of a page. This does not necessarily mean that the element is visible.
??? locator - used to find the element
??? returns the WebElement once it is located
??? """
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? return _find_element(driver, self.locator)
class visibility_of_element_located(object):
??? """ An expectation for checking that an element is present on the DOM of a
??? page and visible. Visibility means that the element is not only displayed
??? but also has a height and width that is greater than 0.
??? locator - used to find the element
??? returns the WebElement once it is located and visible
??? """
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? try:
??????????? return _element_if_visible(_find_element(driver, self.locator))
??????? except StaleElementReferenceException:
??????????? return False
class visibility_of(object):
??? """ An expectation for checking that an element, known to be present on the
??? DOM of a page, is visible. Visibility means that the element is not only
??? displayed but also has a height and width that is greater than 0.
??? element is the WebElement
??? returns the (same) WebElement once it is visible
??? """
??? def __init__(self, element):
??????? self.element = element
??? def __call__(self, ignored):
??????? return _element_if_visible(self.element)
def _element_if_visible(element, visibility=True):
??? return element if element.is_displayed() == visibility else False
class presence_of_all_elements_located(object):
??? """ An expectation for checking that there is at least one element present
??? on a web page.
??? locator is used to find the element
??? returns the list of WebElements once they are located
??? """
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? return _find_elements(driver, self.locator)
class visibility_of_any_elements_located(object):
??? """ An expectation for checking that there is at least one element visible
??? on a web page.
??? locator is used to find the element
??? returns the list of WebElements once they are located
??? """
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? return [element for element in _find_elements(driver, self.locator) if _element_if_visible(element)]
class text_to_be_present_in_element(object):
??? """ An expectation for checking if the given text is present in the
??? specified element.
??? locator, text
??? """
??? def __init__(self, locator, text_):
??????? self.locator = locator
??????? self.text = text_
??? def __call__(self, driver):
??????? try:
??????????? element_text = _find_element(driver, self.locator).text
??????????? return self.text in element_text
??????? except StaleElementReferenceException:
??????????? return False
class text_to_be_present_in_element_value(object):
??? """
??? An expectation for checking if the given text is present in the element's
??? locator, text
??? """
??? def __init__(self, locator, text_):
??????? self.locator = locator
??????? self.text = text_
??? def __call__(self, driver):
??????? try:
??????????? element_text = _find_element(driver,
???????????????????????????????????????? self.locator).get_attribute("value")
??????????? if element_text:
??????????????? return self.text in element_text
??????????? else:
??????????????? return False
??????? except StaleElementReferenceException:
??????????????? return False
class frame_to_be_available_and_switch_to_it(object):
??? """ An expectation for checking whether the given frame is available to
??? switch to.? If the frame is available it switches the given driver to the
??? specified frame.
??? """
??? def __init__(self, locator):
??????? self.frame_locator = locator
??? def __call__(self, driver):
??????? try:
??????????? if isinstance(self.frame_locator, tuple):
??????????????? driver.switch_to.frame(_find_element(driver,
???????????????????????????????????????????????????? self.frame_locator))
??????????? else:
??????????????? driver.switch_to.frame(self.frame_locator)
??????????? return True
??????? except NoSuchFrameException:
??????????? return False
class invisibility_of_element_located(object):
??? """ An Expectation for checking that an element is either invisible or not
??? present on the DOM.
??? locator used to find the element
??? """
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? try:
??????????? return _element_if_visible(_find_element(driver, self.locator), False)
??????? except (NoSuchElementException, StaleElementReferenceException):
??????????? # In the case of NoSuchElement, returns true because the element is
??????????? # not present in DOM. The try block checks if the element is present
??????????? # but is invisible.
??????????? # In the case of StaleElementReference, returns true because stale
??????????? # element reference implies that element is no longer visible.
??????????? return True
class element_to_be_clickable(object):
??? """ An Expectation for checking an element is visible and enabled such that
??? you can click it."""
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? element = visibility_of_element_located(self.locator)(driver)
??????? if element and element.is_enabled():
??????????? return element
??????? else:
??????????? return False
class staleness_of(object):
??? """ Wait until an element is no longer attached to the DOM.
??? element is the element to wait for.
??? returns False if the element is still attached to the DOM, true otherwise.
??? """
??? def __init__(self, element):
??????? self.element = element
??? def __call__(self, ignored):
??????? try:
??????????? # Calling any method forces a staleness check
??????????? self.element.is_enabled()
??????????? return False
??????? except StaleElementReferenceException:
??????????? return True
class element_to_be_selected(object):
??? """ An expectation for checking the selection is selected.
??? element is WebElement object
??? """
??? def __init__(self, element):
??????? self.element = element
??? def __call__(self, ignored):
??????? return self.element.is_selected()
class element_located_to_be_selected(object):
??? """An expectation for the element to be located is selected.
??? locator is a tuple of (by, path)"""
??? def __init__(self, locator):
??????? self.locator = locator
??? def __call__(self, driver):
??????? return _find_element(driver, self.locator).is_selected()
class element_selection_state_to_be(object):
??? """ An expectation for checking if the given element is selected.
??? element is WebElement object
??? is_selected is a Boolean."
??? """
??? def __init__(self, element, is_selected):
??????? self.element = element
??????? self.is_selected = is_selected
??? def __call__(self, ignored):
??????? return self.element.is_selected() == self.is_selected
class element_located_selection_state_to_be(object):
??? """ An expectation to locate an element and check if the selection state
??? specified is in that state.
??? locator is a tuple of (by, path)
??? is_selected is a boolean
??? """
??? def __init__(self, locator, is_selected):
??????? self.locator = locator
??????? self.is_selected = is_selected
??? def __call__(self, driver):
??????? try:
??????????? element = _find_element(driver, self.locator)
??????????? return element.is_selected() == self.is_selected
??????? except StaleElementReferenceException:
??????????? return False
class alert_is_present(object):
??? """ Expect an alert to be present."""
??? def __init__(self):
??????? pass
??? def __call__(self, driver):
??????? try:
??????????? alert = driver.switch_to.alert
??????????? alert.text
??????????? return alert
??????? except NoAlertPresentException:
??????????? return False
def _find_element(driver, by):
??? """Looks up an element. Logs and re-raises ``WebDriverException``
??? if thrown."""
??? try:
??????? return driver.find_element(*by)
??? except NoSuchElementException as e:
??????? raise e
??? except WebDriverException as e:
??????? raise e
def _find_elements(driver, by):
??? try:
??????? return driver.find_elements(*by)
??? except WebDriverException as e:
??????? raise e
```
- Linux
- Linux 文件權限概念
- 重點總結
- Linux 文件與目錄管理
- 2.1 文件與目錄管理
- 2.2 文件內容查閱
- 文件與文件系統的壓縮,打包與備份
- 3.1 Linux 系統常見的壓縮指令
- 3.2 打包指令: tar
- vi/vim 程序編輯器
- 4.1 vi 的使用
- 4.2 vim編輯器刪除一行或者多行內容
- 進程管理
- 5.1 常用命令使用技巧
- 5.2 進程管理
- 系統服務 (daemons)
- 6.1 通過 systemctl 管理服務
- Linux 系統目錄結構
- Linux yum命令
- linux系統查看、修改、更新系統時間(自動同步網絡時間)
- top linux下的任務管理器
- Linux基本配置
- CentOS7開啟防火墻
- CentOS 使用yum安裝 pip
- strace 命令
- Linux下設置固定IP地址
- 查看Linux磁盤及內存占用情況
- Mysql
- 關系數據庫概述
- 數據庫技術
- 數據庫基礎語句
- 查詢語句(--重點--)
- 約束
- 嵌套查詢(子查詢)
- 表emp
- MySQL數據庫練習
- 01.MySQL數據庫練習數據
- 02.MySQL數據庫練習題目
- 03.MySQL數據庫練習-答案
- Mysql遠程連接數據庫
- Python
- python基礎
- Python3中字符串、列表、數組的轉換方法
- python字符串
- python安裝、pip基本用法、變量、輸入輸出、流程控制、循環
- 運算符及優先級、數據類型及常用操作、深淺拷貝
- 虛擬環境(virtualenv)
- 網絡編程
- TCP/IP簡介
- TCP編程
- UDP編程
- 進程和線程
- 訪問數據庫
- 使用SQLite
- 使用MySQL
- Web開發
- HTML簡介
- Python之日志處理(logging模塊)
- 函數式編程
- 高階函數
- python報錯解決
- 啟動Python時報“ImportError: No module named site”錯誤
- python實例
- 01- 用python解決數學題
- 02- 冒泡排序
- 03- 郵件發送(smtplib)
- Django
- 01 Web應用
- Django3.2 教程
- Django簡介
- Django環境安裝
- 第一個Django應用
- Part 1:請求與響應
- Part 2:模型與后臺
- Part 3:視圖和模板
- Part 4:表單和類視圖
- Part 5:測試
- Part 6:靜態文件
- Part 7:自定義admin
- 第一章:模型層
- 實戰一:基于Django3.2可重用登錄與注冊系統
- 1. 搭建項目環境
- 2. 設計數據模型
- 3. admin后臺
- 4. url路由和視圖
- 5. 前端頁面設計
- 6. 登錄視圖
- 7. Django表單
- 8. 圖片驗證碼
- 9. session會話
- 10. 注冊視圖
- 實戰二:Django3.2之CMDB資產管理系統
- 1.項目需求分析
- 2.模型設計
- 3.數據收集客戶端
- 4.收集Windows數據
- 5.Linux下收集數據
- 6.新資產待審批區
- 7.審批新資產
- django 快速搭建blog
- imooc-Django全棧項目開發實戰
- redis
- 1.1 Redis簡介
- 1.2 安裝
- 1.3 配置
- 1.4 服務端和客戶端命令
- 1.5 Redis命令
- 1.5.1 Redis命令
- 1.5.2 鍵(Key)
- 1.5.3 字符串(string)
- 1.5.4 哈希(Hash)
- 1.5.5 列表(list)
- 1.5.6 集合(set)
- 1.5.7 有序集合(sorted set)
- Windows
- Win10安裝Ubuntu子系統
- win10遠程桌面身份驗證錯誤,要求的函數不受支持
- hm軟件測試
- 02 linux基本命令
- Linux終端命令格式
- Linux基本命令(一)
- Linux基本命令(二)
- 02 數據庫
- 數據庫簡介
- 基本概念
- Navicat使用
- SQL語言
- 高級
- 03 深入了解軟件測試
- day01
- 04 python基礎
- 語言基礎
- 程序中的變量
- 程序的輸出
- 程序中的運算符
- 數據類型基礎
- 數據序列
- 數據類型分類
- 字符串
- 列表
- 元組
- 字典
- 列表與元組的區別詳解
- 函數
- 案例綜合應用
- 列表推導式
- 名片管理系統
- 文件操作
- 面向對象基礎(一)
- 面向對象基礎(二)
- 異常、模塊
- 05 web自動化測試
- Day01
- Day02
- Day03
- Day04
- Day05
- Day06
- Day07
- Day08
- 06 接口自動化測試
- 軟件測試面試大全2020
- 第一章 測試理論
- 軟件測試面試
- 一、軟件基礎知識
- 二、網絡基礎知識
- 三、數據庫
- SQL學生表 — 1
- SQL學生表 — 2
- SQL查詢 — 3
- SQL經典面試題 — 4
- 四、linux
- a. linux常用命令
- 五、自動化測試
- 自動化測試
- python 筆試題
- selenium面試題
- 如何判斷一個頁面上元素是否存在?
- 如何提高腳本的穩定性?
- 如何定位動態元素?
- 如何通過子元素定位父元素?
- 如果截取某一個元素的圖片,不要截取全部圖片
- 平常遇到過哪些問題?如何解決的
- 一個元素明明定位到了,點擊無效(也沒報錯),如果解決?
- selenium中隱藏元素如何定位?(hidden、display: none)
- 六、接口測試
- 接口測試常規面試題
- 接口自動化面試題
- json和字典dict的區別?
- 測試的數據你放在哪?
- 什么是數據驅動,如何參數化?
- 下個接口請求參數依賴上個接口的返回數據
- 依賴于登錄的接口如何處理?
- 依賴第三方的接口如何處理
- 不可逆的操作,如何處理,比如刪除一個訂單這種接口如何測試
- 接口產生的垃圾數據如何清理
- 一個訂單的幾種狀態如何全部測到,如:未處理,處理中,處理失敗,處理成功
- python如何連接數據庫操作?
- 七、App測試
- 什么是activity?
- Activity生命周期?
- Android四大組件
- app測試和web測試有什么區別?
- android和ios測試區別?
- app出現ANR,是什么原因導致的?
- App出現crash原因有哪些?
- app對于不穩定偶然出現anr和crash時候你是怎么處理的?
- app的日志如何抓取?
- logcat查看日志步驟
- 你平常會看日志嗎, 一般會出現哪些異常
- 抓包工具
- fiddler
- Wireshark
- 安全/滲透測試
- 安全性測試都包含哪些內容?
- 開放性思維題
- 面試題
- 字節測試面試
- 一、計算機網絡
- 二、操作系統
- 三、數據庫
- 四、數據結構與算法
- 五、Python
- 六、Linux
- 七、測試用例
- 八、智力/場景題
- 九、開放性問題
- python3_收集100+練習題(面試題)
- python3_100道題目答案
- 接口測試
- 接口測試實例_01
- python+requests接口自動化測試框架實例詳解
- 性能測試
- 性能測試流程
- 性能測試面試題
- 如何編寫性能測試場景用例
- 性能測試:TPS和QPS的區別
- jmeter
- jmeter安裝配置教程
- Jmeter性能測試 入門
- PyCharm
- 快捷工具
- 1-MeterSphere
- 一、安裝和升級
- 2- MobaXterm 教程
- 3-fiddler抓包
- 4-Xshell
- Xshell的安裝和使用
- Xshell遠程連接失敗怎么解決
- 5-Vmware
- Vmware提示以獨占方式鎖定此配置文件失敗
- Windows10徹底卸載VMWare虛擬機步驟
- VM ware無法關機,虛擬機繁忙
- VMware虛擬機下載與安裝
- 解決VM 與 Device/Credential Guard 不兼容。在禁用 Device/Credential Guard 后,可以運行 VM 的方法
- VMware虛擬機鏡像克隆與導入
- 6-WPS
- 1.WPS文檔里的批注怎么刪除
- 2.wps表格中設置圖表的坐標
- 3. wps快速繪制數學交集圖
- 7-MongoDB
- Win10安裝配置MongoDB
- Navicat 15.x for MongoDB安裝破解教程
- Apache
- apache層的賬戶權限控制,以及apache黑名單白名單過濾功能
- HTTP / HTTPS協議
- HTTP協議詳解
- 代理
- 狀態碼詳解
- HTTPS詳解
- Selenium3+python3
- (A) selenium
- selenium自動化環境搭建(Windows10)
- 火狐firebug和firepath插件安裝方法(最新)
- 元素定位工具和方法
- Selenium3+python3自動化
- 新手學習selenium路線圖---學前篇
- 1-操作瀏覽器基本方法
- 2-八種元素定位方法
- 3-CSS定位語法
- 4-登錄案例
- 5-定位一組元素find_elements
- 6-操作元素(鍵盤和鼠標事件)
- 7-多窗口、句柄(handle)
- 8-iframe
- 9-select下拉框
- 10-alert\confirm\prompt
- 11-JS處理滾動條
- 12-單選框和復選框(radiobox、checkbox)
- 13-js處理日歷控件(修改readonly屬性)
- 14-js處理內嵌div滾動條
- 15-table定位
- 16-js處理多窗口
- 17-文件上傳(send_keys)
- 18-獲取百度輸入聯想詞
- 19-處理瀏覽器彈窗
- 20-獲取元素屬性
- 21-判斷元素存在
- 22-爬頁面源碼(page_source)
- 23-顯式等待(WebDriverWait)
- 24-關于面試的題
- 25-cookie相關操作
- 26-判斷元素(expected_conditions)
- 27-判斷title(title_is)
- 28-元素定位參數化(find_element)
- 29-18種定位方法(find_elements)
- 30- js解決click失效問題
- 31- 判斷彈出框存在(alert_is_present)
- 32- 登錄方法(參數化)
- 33- 判斷文本(text_to_be_present_in_element)
- 34- unittest簡介
- 35- unittest執行順序
- 36- unittest之裝飾器(@classmethod)
- 37- unittest之斷言(assert)
- 38- 捕獲異常(NoSuchElementException)
- 39- 讀取Excel數據(xlrd)
- 40- 數據驅動(ddt)
- 41- 異常后截圖(screenshot)
- 42- jenkins持續集成環境搭建
- 43- Pycharm上python和unittest兩種運行方式
- 44- 定位的坑:class屬性有空格
- 45- 只截某個元素的圖
- 46- unittest多線程執行用例
- 47- unittest多線程生成報告(BeautifulReport)
- 48- 多線程啟動多個不同瀏覽器
- (B) python3+selenium3實現web UI功能自動化測試框架
- (C) selenium3常見報錯處理
- 書籍
- (D)Selenium3自動化測試實戰--基于Python語
- 第4章 WebDriver API
- 4.1 從定位元素開始
- 4.2 控制瀏覽器
- 4.3 WebDriver 中的常用方法
- 4.4 鼠標操作
- 4.5 鍵盤操作
- 4.6 獲得驗證信息
- 4.7 設置元素等待
- 4.8 定位一組元素
- 4.9 多表單切換
- 4.10 多窗口切換
- 4.11 警告框處理
- 4.12 下拉框處理
- 4.13 上傳文件
- 4.14 下載文件
- 4.15 操作cookie
- 4.16 調用JavaScript
- 4.17 處理HTML5視頻播放
- 4.18 滑動解鎖
- 4.19 窗口截圖
- 第5章 自動化測試模型
- 5.3 模塊化與參數化
- 5.4 讀取數據文件
- 第6章 unittest單元測試框架
- 6.1 認識unittest