判斷元素16種方法expected_conditions


經常有小伙伴問,如何判斷一個元素是否存在,如何判斷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

image.png-13.1kB

判斷title的方法:title_is

1.獲取title。我們可以通過:driver.title獲取,然后也可以把獲取的結果用作斷言
2.這里介紹另外一種方法,主要是判斷頁面title與期望的結果是否一樣,這里就用到了:expected_conditions模塊里的title_is和title_contains兩種方法:

一、源碼分析
1.首先看下源碼,如下

  1.  
    class title_is(object):
  2.  
    """An expectation for checking the title of a page.
  3.  
    title is the expected title, which must be an exact match
  4.  
    returns True if the title matches, false otherwise."""
  5.  
    '''翻譯:檢查頁面的title與期望值是都完全一致,如果完全一致,返回Ture,否則返回Flase'''
  6.  
    def __init__(self, title):
  7.  
    self.title = title
  8.  
    def __call__(self, driver):
  9.  
    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,返回結果:是布爾值

  1.  
    #coding:utf-8
  2.  
    from selenium import webdriver
  3.  
    from selenium.webdriver.support import expected_conditions as EC
  4.  
    driver = webdriver.Chrome()
  5.  
    driver.get( "http://www.cnblogs.com/surewing")
  6.  
    title = EC.title_is( u"cherry小櫻桃 - 博客")
  7.  
    print(title(driver))

三、判斷title包含:title_contains
1.這個類跟上面那個類差不多,只是這個是部分匹配(類似於xpath里面的contains語法)
2.判斷title包含'小櫻桃'字符串

  1.  
    #coding:utf-8
  2.  
    from selenium import webdriver
  3.  
    from selenium.webdriver.support import expected_conditions as EC
  4.  
    driver = webdriver.Chrome()
  5.  
    driver.get( "http://www.cnblogs.com/surewing")
  6.  
    title = EC.title_contains( u"小櫻桃")
  7.  
    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.判斷百度首頁上,“用戶名”按鈕這個元素中存在文本:用戶名

image.png-135kB
2.locator參數是定位方法
3.text是期望值

  1.  
    #coding:utf-8
  2.  
    from selenium import webdriver
  3.  
    from selenium.webdriver.support import expected_conditions as EC
  4.  
    driver =webdriver.Chrome()
  5.  
    url= "http://www.baidu.com"
  6.  
    driver.get(url)
  7.  
    locator =( "id","s_username_top")
  8.  
    text = u"哦啊啊4yv7"
  9.  
    result = EC.text_to_be_present_in_element_value(locator,text)(driver)
  10.  
    print(result)

三、失敗的案例:
1.如果判斷失敗,就返回False

  1.  
    locator =( "id","s_username_top")
  2.  
    text= u"哦啊啊4yv7"
  3.  
    result = EC.text_to_be_present_in_element(locator,text)(driver)
  4.  
    print(result)
  5.  
    text1 = u"kkkk"
  6.  
    result = EC.text_to_be_present_in_element_value(locator,text1)(driver)
  7.  
    print(result1)

判斷彈框出現alert_is_present

系統彈框我們會經常見到,有時候他不彈出來去操作的話,會有異常現象產生,那么又不知道他什么時候出現,那么就需要去判斷彈框是否出現了。
二、實例操作
1.前面的操作步驟優化了下,為了提高腳本的穩定性,確保元素出現后操作,這里結合WebDriverWait里的方法
2.實現步驟如下,這里判斷的結果返回有兩種:沒找到就返回False;找到就返回alert對象
3.先判斷alert是否彈出,如果彈出就點確定按鈕accept()

  1.  
    mouse = webDriverWait(driver, 10).until(lambda x:x.find_element("link text","設置"))
  2.  
    ActionChains(driver).move_to_element(mouse).perform()
  3.  
    webDriverWait(driver, 10).until(lambda x:x.find_element("link text","搜索設置")).click()
  4.  
    #選擇設置項
  5.  
    s=WebDriverWait(driver, 10).until(lambda x:x.find_element('id','nr'))
  6.  
    Select(s).select_by_visible_text( "每頁顯示50條")
  7.  
    #點擊保存按鈕
  8.  
    js = "document.getElemensByClassName('prefpanelgo')[0].click();"
  9.  
    driver.execute_script(js)
  10.  
    result = EC.alert_is_present()(driver)
  11.  
    if result:
  12.  
    print(result.text)
  13.  
    result.accept()
  14.  
    else:
  15.  
    print( "alert 未彈出")

封裝:

我們學了顯示等待后,就不需要sleep了,然后查找元素方法用參數化去定位,這樣定位方法更靈活了,但是這樣寫起來代碼會很長了,於是問題來了,總不能每次定位一個元素都要寫一大堆代碼吧?這時候就要學會封裝啦!

一、顯示等待
1.如果你的定位元素代碼,還是這樣:driver.find_element_by_id("kw").send_keys("百度"),那說明你還停留在小學水平,如何讓代碼提升逼格呢?
2.前面講過顯示等待相對於sleep來說更省時間,定位元素更靠譜,不會出現一會正常運行,一會又報錯的情況,所以我們的定位需與WebDriverWait結合
3.以百度的搜索為例

  1.  
    # coding:utf-8
  2.  
    from selenium import webdriver
  3.  
    from selenium.webdriver.support import expected_conditions as EC
  4.  
    from selenium.webdriver.support.ui import webDriverWait
  5.  
     
  6.  
    driver =webdriver.Chrome()
  7.  
    driver.get( "http://www.baidu.com")
  8.  
    #輸入搜索的內容
  9.  
    input_loc = ( "id","kw")
  10.  
    WebDriverWait(driver, 10).until(lambda x:x.find_element(*input_loc)).send_keys("bbb")
  11.  
    #點擊搜索按鈕
  12.  
    button_loc = ( "id","su")
  13.  
    WebDriverWait(driver, 10).until(lambda x:x.find_element(*input_loc)).click()

二、封裝定位方法
1.從上面代碼看太長了,每次定位寫一大串,這樣不方便閱讀,寫代碼的效率也低,於是我們可以把定位方法進行封裝
2.定位方法封裝后,我們每次調用自己寫的方法就方便多了
image.png-80.1kB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM