Selenium 异常处理

在使用 Selenium 的过程中,难免会遇到一些异常,例如超时、节点未找到等错误,我们可以使用 try...except... 语句来捕获各种异常

更多异常类参考官网:https://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions

from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException

browser = webdriver.Chrome()

try:
    browser.get("http://www.baidu.com")    # 打开浏览器访问百度
except TimeoutException:                   # 捕获是否超时
    print("Time Out.")

try:
    browser.find_element_by_id("hello")    # 查找指定元素
except NoSuchElementException:             # 捕获是否找不到元素
    print("No Element.")
finally:
    browser.close()

 

 

 

 

 

 

 

 

      

posted @ 2019-03-18 11:05  孔雀东南飞  阅读(1354)  评论(0编辑  收藏  举报