27 lines
596 B
Python
27 lines
596 B
Python
# -*- encoding:utf-8 -*-
|
|
|
|
'''
|
|
@Author : dingjiawen
|
|
@Date : 2023/12/6 21:17
|
|
@Usage : 异常处理
|
|
@Desc : 可能会遇到获取节点失败的异常,可以对异常进行处理
|
|
'''
|
|
|
|
from selenium import webdriver
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.common.exceptions import TimeoutException, NoSuchElementException
|
|
|
|
browser = webdriver.Chrome()
|
|
|
|
try:
|
|
browser.get('https://www.baidu.com')
|
|
except TimeoutException:
|
|
print('Time out')
|
|
|
|
try:
|
|
browser.find_element(By.ID, 'hello')
|
|
except NoSuchElementException:
|
|
print('No Such Element')
|
|
finally:
|
|
browser.close()
|