【问题标题】:selenium python page down - unknown error: net::ERR_NAME_NOT_RESOLVEDselenium python page down - 未知错误:net::ERR_NAME_NOT_RESOLVED
【发布时间】:2020-12-19 16:58:20
【问题描述】:

所以我目前正在开发一个 python 刮板,以在 python 中使用 selenium 收集网站信息。我遇到的问题是,如果我转到一个不存在的页面,我会收到错误消息:unknown error: net::ERR_NAME_NOT_RESOLVED

我有一段时间没用过python,所以我的知识不是最好的。

这是我的代码

driver = webdriver.Chrome(ChromeDriverManager().install())
try:
    driver.get('%s' %link)
except ERR_NAME_NOT_RESOLVED:
    print ("page down")

示例网站:http://www.whitefoxcatering.co.uk

错误

Traceback (most recent call last):
  File "C:\Users\STE\AppData\Local\Programs\Python\Python39\selenium email\selenium test.py", line 106, in <module>
    driver.get('%s' %url)
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: chrome=87.0.4280.88)

我已经搜索过之前的问题,但似乎无法找到解决方案。

任何帮助将不胜感激:)

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver


    【解决方案1】:

    如果您想捕捉错误,您必须将您的except 更改为 selenium 为您提供的内容。在这种情况下:selenium.common.exceptions.WebDriverException

    所以先导入它:

    from selenium.common.exceptions import WebDriverException
    

    那你就可以抓到了:

    try:
        driver.get('http://www.whitefoxcatering.co.uk')
    except WebDriverException:
        print("page down")
    

    【讨论】:

      猜你喜欢
      • 2022-08-23
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 2018-04-28
      • 2015-10-06
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多