經常有小伙伴問,如何判斷一個元素是否存在,如何判斷alert彈窗出來了,如何判斷動態的元素等等一系列的判斷,在selenium的expected_conditions模塊收集了一系列的場景判斷方法,這些方法是逢面試必考的!!!
- expected_conditions 一般也簡稱為EC,本篇先介紹下有哪些功能;
一、功能介紹和翻譯:
- 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
判斷title的方法:title_is
1.獲取title。我們可以通過:driver.title獲取,然后也可以把獲取的結果用作斷言
2.這里介紹另外一種方法,主要是判斷頁面title與期望的結果是否一樣,這里就用到了:expected_conditions模塊里的title_is和title_contains兩種方法:
一、源碼分析
1.首先看下源碼,如下
-
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."""
-
'''翻譯:檢查頁面的title與期望值是都完全一致,如果完全一致,返回Ture,否則返回Flase'''
-
def __init__(self, title):
-
self.title = title
-
def __call__(self, driver):
-
return self.title == driver.title
2.注釋翻譯:檢查頁面的title與期望值是都完全一致,如果完全一致,返回True,否則返回Flase
3.title_is()這個是一個class類型,里面有兩個方法
4.__init__是初始化內容,參數是title,必填項
5.__call__是把實例變成一個對象,參數是driver,返回的是self.title == driver.title,布爾值
二、判斷title:tile_is()
1.首先導入expected_conditions模塊
2.由於這個模塊名字比較長,為了方便調用我們重名了EC
3.打開博客頁面判斷title,返回結果:是布爾值
-
#coding:utf-8
-
from selenium import webdriver
-
from selenium.webdriver.support import expected_conditions as EC
-
driver = webdriver.Chrome()
-
driver.get( "http://www.cnblogs.com/surewing")
-
title = EC.title_is( u"cherry小櫻桃 - 博客")
-
print(title(driver))
三、判斷title包含:title_contains
1.這個類跟上面那個類差不多,只是這個是部分匹配(類似於xpath里面的contains語法)
2.判斷title包含'小櫻桃'字符串
-
#coding:utf-8
-
from selenium import webdriver
-
from selenium.webdriver.support import expected_conditions as EC
-
driver = webdriver.Chrome()
-
driver.get( "http://www.cnblogs.com/surewing")
-
title = EC.title_contains( u"小櫻桃")
-
print(title(driver))
判斷文本text_to_be_present_in_element
在做結果判斷的時候,經常想判斷某個元素中是否存在指定的文本,如登錄后判斷頁面中是賬號是否是該用戶的用戶名。
在前面的登錄案例中,寫了一個簡單的方法,但不是公用的,在EC模塊有個方法是可以專門用來判斷元素中存在指定文本的:text_to_be_present_in_element。
另外一個差不多復方法判斷元素的value值:text_to_be_present_in_element_value。
二、判斷文本
1.判斷百度首頁上,“用戶名”按鈕這個元素中存在文本:用戶名
2.locator參數是定位方法
3.text是期望值
-
#coding:utf-8
-
from selenium import webdriver
-
from selenium.webdriver.support import expected_conditions as EC
-
driver =webdriver.Chrome()
-
url= "http://www.baidu.com"
-
driver.get(url)
-
locator =( "id","s_username_top")
-
text = u"哦啊啊4yv7"
-
result = EC.text_to_be_present_in_element_value(locator,text)(driver)
-
print(result)
三、失敗的案例:
1.如果判斷失敗,就返回False
-
locator =( "id","s_username_top")
-
text= u"哦啊啊4yv7"
-
result = EC.text_to_be_present_in_element(locator,text)(driver)
-
print(result)
-
text1 = u"kkkk"
-
result = EC.text_to_be_present_in_element_value(locator,text1)(driver)
-
print(result1)
判斷彈框出現alert_is_present
系統彈框我們會經常見到,有時候他不彈出來去操作的話,會有異常現象產生,那么又不知道他什么時候出現,那么就需要去判斷彈框是否出現了。
二、實例操作
1.前面的操作步驟優化了下,為了提高腳本的穩定性,確保元素出現后操作,這里結合WebDriverWait里的方法
2.實現步驟如下,這里判斷的結果返回有兩種:沒找到就返回False;找到就返回alert對象
3.先判斷alert是否彈出,如果彈出就點確定按鈕accept()
-
mouse = webDriverWait(driver, 10).until(lambda x:x.find_element("link text","設置"))
-
ActionChains(driver).move_to_element(mouse).perform()
-
webDriverWait(driver, 10).until(lambda x:x.find_element("link text","搜索設置")).click()
-
#選擇設置項
-
s=WebDriverWait(driver, 10).until(lambda x:x.find_element('id','nr'))
-
Select(s).select_by_visible_text( "每頁顯示50條")
-
#點擊保存按鈕
-
js = "document.getElemensByClassName('prefpanelgo')[0].click();"
-
driver.execute_script(js)
-
result = EC.alert_is_present()(driver)
-
if result:
-
print(result.text)
-
result.accept()
-
else:
-
print( "alert 未彈出")
封裝:
我們學了顯示等待后,就不需要sleep了,然后查找元素方法用參數化去定位,這樣定位方法更靈活了,但是這樣寫起來代碼會很長了,於是問題來了,總不能每次定位一個元素都要寫一大堆代碼吧?這時候就要學會封裝啦!
一、顯示等待
1.如果你的定位元素代碼,還是這樣:driver.find_element_by_id("kw").send_keys("百度"),那說明你還停留在小學水平,如何讓代碼提升逼格呢?
2.前面講過顯示等待相對於sleep來說更省時間,定位元素更靠譜,不會出現一會正常運行,一會又報錯的情況,所以我們的定位需與WebDriverWait結合
3.以百度的搜索為例
-
# coding:utf-8
-
from selenium import webdriver
-
from selenium.webdriver.support import expected_conditions as EC
-
from selenium.webdriver.support.ui import webDriverWait
-
-
driver =webdriver.Chrome()
-
driver.get( "http://www.baidu.com")
-
#輸入搜索的內容
-
input_loc = ( "id","kw")
-
WebDriverWait(driver, 10).until(lambda x:x.find_element(*input_loc)).send_keys("bbb")
-
#點擊搜索按鈕
-
button_loc = ( "id","su")
-
WebDriverWait(driver, 10).until(lambda x:x.find_element(*input_loc)).click()
二、封裝定位方法
1.從上面代碼看太長了,每次定位寫一大串,這樣不方便閱讀,寫代碼的效率也低,於是我們可以把定位方法進行封裝
2.定位方法封裝后,我們每次調用自己寫的方法就方便多了