学习笔记 捕获异常

1可以用try...except...捕获异常

2从selenium.common.exceptions导入NoSuchElementException类

#coding:utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver=webdriver.Firefox()
driver.get("https://www.cnblogs.com/caojuansh/")
try:
element=driver.find_element("id","blog_nav_newpostxx")
except NoSuchElementException as msg:
print(u"查找元素异常%s"%msg)
else:
element.click()

 常见异常:在Lib目录下:selenium/common/exceptions

1 NoSuchElementException 没有找到元素

2 NoSuchFrameException 没有找到iframe

3 NoSuchWindowException 没找到窗口句柄handle

4 NoSuchAttributeException 属性错误

5 NoAlertPresentException 没找到alert弹出框

6 ElementNotVisibleException 元素不可见

7 ElementNotSelectableException 元素没有被选中

8 TimeoutException 查找元素超时

原文地址:https://www.cnblogs.com/caojuansh/p/14607004.html