Selenium2+python自動化43-判斷title(title_is)

來源:互聯網
上載者:User

標籤:width   cte   www   自動化   模組名   print   www.   字元   htm   

前言

擷取頁面title的方法可以直接用driver.title擷取到,然後也可以把擷取到的結果用做斷言。

本篇介紹另外一種方法去判斷頁面title是否與期望結果一種,用到上一篇Selenium2+python自動化42-判斷元素(expected_conditions)

提到的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:title_is()

1.首先匯入expected_conditions模組

2.由於這個模組名稱比較長,所以為了後續的調用方便,重新命名為EC了(有點像資料庫裡面多表查詢時候重新命名)

3.開啟部落格首頁後判斷title,返回結果是True或False

 

三、判斷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.Firefox()
driver.get("http://www.cnblogs.com/yoyoketang")
# 判斷title完全等於
title = EC.title_is(u‘上海-悠悠 - 部落格園‘)
print title(driver)

# 判斷title包含
title1 = EC.title_contains(u‘上海-悠悠‘)
print title1(driver)

# 另外一種寫法
r1 = EC.title_is(u‘上海-悠悠 - 部落格園‘)(driver)
r2 = EC.title_contains(u‘上海-悠悠‘)(driver)
print r1
print r2

Selenium2+python自動化43-判斷title(title_is)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.