通过try except语句块来进行测试断言
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(“http://www.baidu.com“)
try:
driver.find_element_by_id(“aa”)#正确的百度搜索框的id为“kw”
print(‘test pass:ID found’)
except Exception as e:
print(“Exception found”,format(e))
driver.quit()
运行结果如下:
Exception found Message: Unable to locate element: [id=”aa”]