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

转载于:https://www.cnblogs.com/wangyinghao/p/10571059.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值