在使用 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()

 

 

 

 

 

 

 

 

      

相关文章:

  • 2021-12-12
  • 2021-07-28
  • 2022-12-23
  • 2021-09-04
  • 2022-02-27
  • 2021-12-18
猜你喜欢
  • 2021-11-14
  • 2021-12-27
  • 2021-06-30
  • 2021-11-24
  • 2021-04-05
  • 2021-06-12
  • 2021-11-13
相关资源
相似解决方案