本文共 643 字,大约阅读时间需要 2 分钟。
本文介绍一个Selenium中页面title断言方法,这个在基础篇中第一个完整的脚本有提到过,这个补充一下。
相关脚本代码如下:
# coding=utf-8import timefrom selenium import webdriverdriver = 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存储。
转载地址:http://ugxws.baihongyu.com/