微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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存储。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐