Python+Selenium練習篇之17-斷言頁面標題


 

繼續來介紹一個Selenium中頁面title斷言方法。

 

相關腳本代碼如下:

 

# coding=utf-8

import time

from selenium import webdriver

 

driver = webdriver.Chrome()

driver.maximize_window()

driver.get('https://www.baidu.com')

time.sleep(1)

# 方法一

try:

    assert u"百度一下" in driver.title

    print ('Assertion test pass.')

except Exception as e:

    print ('Assertion test fail.', format(e))

# 方法二

if u"百度一下,你就知道" == driver.title :

    print ('Assertion test pass.')

else:

    print ('Assertion test fail.')

 

print driver.title

 

方法一,是利用python中Assert方法,采用包含判斷,方法二是通過if方法,采用完全相等方法,建議選擇第一種方法。

u"百度一下,你就知道"

這u代表unicode的意思,由於我們這里采用了python 2, 如果你使用pyn3 就不需要,在Python3中,字符串默認采用unicode存儲。


免責聲明!

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



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