SergeyPirogov / webdriver_manager

Apache License 2.0
2.05k stars 456 forks source link

遇到打不开火狐浏览器的错误,怎么处理? #608

Closed 89333367 closed 1 year ago

89333367 commented 1 year ago

错误描述

2023-08-29 16:38:52.930 | INFO     | __main__:get_firefox_driver:51 - 尝试打开 firefox 浏览器
2023-08-29 16:38:55.811 | ERROR    | __main__:<module>:55 - An error has been caught in function '<module>', process 'MainProcess' (1428), thread 'MainThread' (37660):
Traceback (most recent call last):

  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
    │    │              └ <urllib3.connection.HTTPSConnection object at 0x0000028F46553C50>
    │    └ <function HTTPSConnectionPool._validate_conn at 0x0000028F462BEDE0>
    └ <urllib3.connectionpool.HTTPSConnectionPool object at 0x0000028F4653FA90>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connectionpool.py", line 1092, in _validate_conn
    conn.connect()
    │    └ <function HTTPSConnection.connect at 0x0000028F4628EB60>
    └ <urllib3.connection.HTTPSConnection object at 0x0000028F46553C50>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connection.py", line 642, in connect
    sock_and_verified = _ssl_wrap_socket_and_match_hostname(
                        └ <function _ssl_wrap_socket_and_match_hostname at 0x0000028F4628D8A0>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connection.py", line 783, in _ssl_wrap_socket_and_match_hostname
    ssl_sock = ssl_wrap_socket(
               └ <function ssl_wrap_socket at 0x0000028F462799E0>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\util\ssl_.py", line 469, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
               │                     │     │        │           └ 'api.github.com'
               │                     │     │        └ False
               │                     │     └ <ssl.SSLContext object at 0x0000028F465440E0>
               │                     └ <socket.socket [closed] fd=-1, family=2, type=1, proto=0>
               └ <function _ssl_wrap_socket_impl at 0x0000028F46279BC0>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\util\ssl_.py", line 513, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           │           │           │                     └ 'api.github.com'
           │           │           └ <socket.socket [closed] fd=-1, family=2, type=1, proto=0>
           │           └ <function SSLContext.wrap_socket at 0x0000028F4286A700>
           └ <ssl.SSLContext object at 0x0000028F465440E0>
  File "C:\Program Files\Python311\Lib\ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           │    │               └ <classmethod(<function SSLSocket._create at 0x0000028F42880680>)>
           │    └ <class 'ssl.SSLSocket'>
           └ <ssl.SSLContext object at 0x0000028F465440E0>
  File "C:\Program Files\Python311\Lib\ssl.py", line 1075, in _create
    self.do_handshake()
    │    └ <function SSLSocket.do_handshake at 0x0000028F42881B20>
    └ <ssl.SSLSocket [closed] fd=-1, family=2, type=1, proto=0>
  File "C:\Program Files\Python311\Lib\ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
    │    └ None
    └ <ssl.SSLSocket [closed] fd=-1, family=2, type=1, proto=0>

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
               │    └ <function HTTPConnectionPool._make_request at 0x0000028F462BE840>
               └ <urllib3.connectionpool.HTTPSConnectionPool object at 0x0000028F4653FA90>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
          └ SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer ...

urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "C:\Program Files\Python311\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           │    └ <function HTTPConnectionPool.urlopen at 0x0000028F462BEA20>
           └ <urllib3.connectionpool.HTTPSConnectionPool object at 0x0000028F4653FA90>
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              │       └ <function Retry.increment at 0x0000028F4623E700>
              └ Retry(total=0, connect=None, read=False, redirect=None, status=None)
  File "C:\Program Files\Python311\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
          │             │      │    │            └ SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer ...
          │             │      │    └ SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer ...
          │             │      └ '/repos/mozilla/geckodriver/releases/latest'
          │             └ <urllib3.connectionpool.HTTPSConnectionPool object at 0x0000028F4653FA90>
          └ <class 'urllib3.exceptions.MaxRetryError'>

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /repos/mozilla/geckodriver/releases/latest (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\http.py", line 32, in get
    resp = requests.get(
           │        └ <function get at 0x0000028F464BE700>
           └ <module 'requests' from 'C:\\Program Files\\Python311\\Lib\\site-packages\\requests\\__init__.py'>
  File "C:\Program Files\Python311\Lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           │              │           │         └ {'verify': True, 'stream': True, 'headers': None}
           │              │           └ None
           │              └ 'https://api.github.com/repos/mozilla/geckodriver/releases/latest'
           └ <function request at 0x0000028F464BE660>
  File "C:\Program Files\Python311\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           │       │              │           │      └ {'params': None, 'verify': True, 'stream': True, 'headers': None}
           │       │              │           └ 'https://api.github.com/repos/mozilla/geckodriver/releases/latest'
           │       │              └ 'get'
           │       └ <function Session.request at 0x0000028F46506660>
           └ <requests.sessions.Session object at 0x0000028F4653FC50>
  File "C:\Program Files\Python311\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           │    │    │       └ {'timeout': None, 'allow_redirects': True, 'proxies': OrderedDict(), 'stream': True, 'verify': True, 'cert': None}
           │    │    └ <PreparedRequest [GET]>
           │    └ <function Session.send at 0x0000028F46506B60>
           └ <requests.sessions.Session object at 0x0000028F4653FC50>
  File "C:\Program Files\Python311\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        │       │    │          └ {'timeout': None, 'proxies': OrderedDict(), 'stream': True, 'verify': True, 'cert': None}
        │       │    └ <PreparedRequest [GET]>
        │       └ <function HTTPAdapter.send at 0x0000028F46505EE0>
        └ <requests.adapters.HTTPAdapter object at 0x0000028F46551350>
  File "C:\Program Files\Python311\Lib\site-packages\requests\adapters.py", line 517, in send
    raise SSLError(e, request=request)
          │                   └ <PreparedRequest [GET]>
          └ <class 'requests.exceptions.SSLError'>

requests.exceptions.SSLError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /repos/mozilla/geckodriver/releases/latest (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

> File "D:\Gitee\jupyter\selenium_demo.py", line 55, in <module>
    driver = get_firefox_driver()
             └ <function get_firefox_driver at 0x0000028F4654C400>

  File "D:\Gitee\jupyter\selenium_demo.py", line 52, in get_firefox_driver
    return webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
           │         │               │              └ <class 'webdriver_manager.firefox.GeckoDriverManager'>
           │         │               └ <class 'selenium.webdriver.firefox.service.Service'>
           │         └ <class 'selenium.webdriver.firefox.webdriver.WebDriver'>
           └ <module 'selenium.webdriver' from 'C:\\Users\\89333\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\...

  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\firefox.py", line 39, in install
    driver_path = self._get_driver_binary_path(self.driver)
                  │    │                       │    └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
                  │    │                       └ <webdriver_manager.firefox.GeckoDriverManager object at 0x0000028F4653E4D0>
                  │    └ <function DriverManager._get_driver_binary_path at 0x0000028F4652C0E0>
                  └ <webdriver_manager.firefox.GeckoDriverManager object at 0x0000028F4653E4D0>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\manager.py", line 35, in _get_driver_binary_path
    binary_path = self._cache_manager.find_driver(driver)
                  │    │              │           └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
                  │    │              └ <function DriverCacheManager.find_driver at 0x0000028F4651B9C0>
                  │    └ <webdriver_manager.core.driver_cache.DriverCacheManager object at 0x0000028F4653E050>
                  └ <webdriver_manager.firefox.GeckoDriverManager object at 0x0000028F4653E4D0>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\driver_cache.py", line 105, in find_driver
    driver_version = self.get_cache_key_driver_version(driver)
                     │    │                            └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
                     │    └ <function DriverCacheManager.get_cache_key_driver_version at 0x0000028F4651BC40>
                     └ <webdriver_manager.core.driver_cache.DriverCacheManager object at 0x0000028F4653E050>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\driver_cache.py", line 151, in get_cache_key_driver_version
    return driver.get_driver_version_to_download()
           │      └ <function Driver.get_driver_version_to_download at 0x0000028F4651B420>
           └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\driver.py", line 48, in get_driver_version_to_download
    return self.get_latest_release_version()
           │    └ <function GeckoDriver.get_latest_release_version at 0x0000028F4652F9C0>
           └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\drivers\firefox.py", line 29, in get_latest_release_version
    resp = self._http_client.get(
           │    │            └ <function WDMHttpClient.get at 0x0000028F4651A980>
           │    └ <webdriver_manager.core.http.WDMHttpClient object at 0x0000028F4653FF10>
           └ <webdriver_manager.drivers.firefox.GeckoDriver object at 0x0000028F4653FE10>
  File "C:\Users\89333\AppData\Roaming\Python\Python311\site-packages\webdriver_manager\core\http.py", line 35, in get
    raise exceptions.ConnectionError(f"Could not reach host. Are you offline?")
          │          └ <class 'requests.exceptions.ConnectionError'>
          └ <module 'requests.exceptions' from 'C:\\Program Files\\Python311\\Lib\\site-packages\\requests\\exceptions.py'>

requests.exceptions.ConnectionError: Could not reach host. Are you offline?

源代码

import os

os.system("python -m pip install --upgrade pip")
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple loguru")
from loguru import logger

import os

os.system("python -m pip install --upgrade pip")
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium")
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple webdriver-manager")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.firefox import GeckoDriverManager

@logger.catch
def get_chrome_driver():
    logger.info("尝试打开 chrome 浏览器")
    options = ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    return webdriver.Chrome(
        service=ChromeService(ChromeDriverManager().install()), options=options
    )

@logger.catch
def get_edge_driver():
    logger.info("尝试打开 edge 浏览器")
    options = EdgeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    return webdriver.Edge(
        service=EdgeService(EdgeChromiumDriverManager().install()), options=options
    )

@logger.catch
def get_firefox_driver():
    logger.info("尝试打开 firefox 浏览器")
    return webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

driver = get_firefox_driver()
driver.maximize_window()

logger.info('关闭浏览器')
driver.quit()
try-agaaain commented 1 year ago

报错的最后一行是网络连接错误:Could not reach host. Are you offline? 是不是因为没有联网呢

DUB1401 commented 1 year ago

I also have this problem. Often.

89333367 commented 1 year ago

肯定联网了,因为edge和chrome都能访问

JokerYF commented 1 year ago

这种问题一般都是国内访问对应版本的下载源超时的问题,找对应版本的代替源吧,或者挂梯,提醒一下,ChromeDriver的官方团队更改了发布新版本的源,国内所有的源都没有办法同步115以上的ChromeDriver,目前我没有找到合适的不挂梯子的解决方案,很坑。。。

qidai77777 commented 1 year ago

这种问题一般都是国内访问对应版本的下载源超时的问题,找对应版本的代替源吧,或者挂梯,提醒一下,ChromeDriver的官方团队更改了发布新版本的源,国内所有的源都没有办法同步115以上的ChromeDriver,目前我没有找到合适的不挂梯子的解决方案,很坑。。。

有别的解决方案或者其他库吗? 我发现我用edge也有这个问题

rtorres90 commented 1 year ago

Hello, I had a similar problem because my employer has a lot of network restrictions. The solution was turn off the webdriver manager SSL verification. To apply this change in your code, please add this block of code in the beginning of your script:

import os

os.environ["WDM_SSL_VERIFY"] = "0"
JokerYF commented 1 year ago

这种问题一般都是国内访问对应版本的下载源超时的问题,找对应版本的代替源吧,或者挂梯,提醒一下,ChromeDriver的官方团队更改了发布新版本的源,国内所有的源都没有办法同步115以上的ChromeDriver,目前我没有找到合适的不挂梯子的解决方案,很坑。。。

有别的解决方案或者其他库吗? 我发现我用edge也有这个问题

我看作者的封装,一般每一个webdriver都有一个源地址,你用edge的话,要不你换个源试试,给你个阿里源的地址https://www.npmmirror.com/