You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未联网时如何让webdriver_manager直接使用缓存中的驱动?

问题:未登录WiFi时Selenium脚本无法通过webdriver_manager安装驱动

我编写了一个用Selenium登录WiFi的Python脚本,但未登录WiFi时设备没有网络访问权限,直接执行dr_mngr().install()会失败——因为webdriver_manager尝试联网下载驱动时,被WiFi的登录页面拦截,返回的是登录HTML页面,导致创建缓存目录时因路径包含非法字符报错。而登录WiFi后再运行脚本,webdriver_manager能从本地缓存读取驱动,脚本可正常工作。

示例代码

from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager as dr_mngr
from subprocess import CREATE_NO_WINDOW
import os

os.environ['WDM_SSL_VERIFY'] = '0'
service = Service(dr_mngr().install())
service.creationflags = CREATE_NO_WINDOW
options = Options()
options.add_argument("--headless")
options.add_argument("--ignore-certificate-errors")
driver = webdriver.Edge(options=options, service=service)
driver.get("http://www.google.com")

执行报错输出

[WDM] - ====== WebDriver manager ======
[WDM] - Current edge version is 103.0.1264
[WDM] - Get LATEST edgedriver version for 103.0.1264 Edge
C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'msedgedriver.azureedge.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host '172.25.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
[WDM] - There is no [win64] edgedriver for browser 103.0.1264 in cache
[WDM] - Trying to download new driver from https://msedgedriver.azureedge.net/<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>
      Fortigate :: Login
    </title>

<!-- HTML Source Code of Login Page here -->

</html>/edgedriver_win64.zip
C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'msedgedriver.azureedge.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host '172.25.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
Traceback (most recent call last):
  File "C:\Users\VirtualBox\LogiNetwork\test.py", line 10, in <module>
    service = Service(dr_mngr().install())
  File "C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\webdriver_manager\microsoft.py", line 61, in install
    driver_path = self._get_driver_path(self.driver)
  File "C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\webdriver_manager\manager.py", line 24, in _get_driver_path
    binary_path = self.driver_cache.save_file_to_cache(driver, file)
  File "C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\webdriver_manager\driver_cache.py", line 32, in save_file_to_cache
    archive = save_file(file, path)
  File "C:\Users\VirtualBox\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\webdriver_manager\utils.py", line 37, in save_file
    os.makedirs(directory, exist_ok=True)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  [Previous line repeated 76 more times]
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\os.py", line 225, in makedirs
    mkdir(name, mode)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\VirtualBox\\.wdm\\drivers\\edgedriver\\win64\\<!DOCTYPE html PUBLIC "-'

解决方案

方案1:提前离线下载驱动,指定本地路径

手动下载对应版本的Edge驱动到本地目录,脚本直接指定驱动路径,跳过webdriver_manager的联网下载流程:

from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from subprocess import CREATE_NO_WINDOW

# 替换为你本地驱动的实际路径
service = Service(r"C:\local_drivers\msedgedriver.exe")
service.creationflags = CREATE_NO_WINDOW
options = Options()
options.add_argument("--headless")
options.add_argument("--ignore-certificate-errors")
driver = webdriver.Edge(options=options, service=service)
driver.get("http://google.com")

方案2:先完成WiFi登录,再初始化webdriver

调整脚本逻辑,先用无驱动依赖的方式(如requests)完成WiFi登录,确保网络正常后再初始化Selenium和webdriver_manager:

import requests
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager as dr_mngr
from subprocess import CREATE_NO_WINDOW
import os

# 根据WiFi认证页面的实际接口调整登录参数
login_data = {
    "username": "你的WiFi账号",
    "password": "你的WiFi密码"
}
# 发送登录请求到WiFi认证服务器
requests.post("http://172.25.0.1/login", data=login_data)

# 登录成功后再初始化webdriver
os.environ['WDM_SSL_VERIFY'] = '0'
service = Service(dr_mngr().install())
service.creationflags = CREATE_NO_WINDOW
options = Options()
options.add_argument("--headless")
options.add_argument("--ignore-certificate-errors")
driver = webdriver.Edge(options=options, service=service)
driver.get("http://google.com")

方案3:预缓存驱动

在有网环境下先运行一次脚本,让webdriver_manager把驱动缓存到本地,之后无网环境下会自动读取缓存,无需再次下载。


内容的提问来源于stack exchange,提问作者Abhishek M J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:15:32