使用Selenium操作Microsoft Edge时遭遇NoSuchDriverException错误求助
问题:Selenium 结合 Microsoft Edge 爬取网页数据失败,触发 NoSuchDriverException
执行webdriver.Edge()时出现以下错误,核心原因是Selenium Manager无法解析EdgeDriver下载服务器的DNS,导致无法自动获取驱动:
PS C:\Users\Queensley\Desktop\hustle> python Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>> browser = webdriver.Edge() Traceback (most recent call last): File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path path = SeleniumManager().driver_location(options) if path is None else path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\common\selenium_manager.py", line 94, in driver_location output = self.run(args) ^^^^^^^^^^^^^^ File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\common\selenium_manager.py", line 140, in run raise WebDriverException(f"Unsuccessful command executed: {command}.\n{result}{stderr}") selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser MicrosoftEdge --output json. {'code': 65, 'message': 'error sending request for url (https://msedgedriver.azureedge.net/LATEST_RELEASE_119_WINDOWS): error trying to connect: dns error: No such host is known. (os error 11001)', 'driver_path': '', 'browser_path': ''}
最终触发上层异常:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__ super().__init__( File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__ self.service.path = DriverFinder.get_path(self.service, options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Queensley\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 41, in get_path raise NoSuchDriverException(msg) from err selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for MicrosoftEdge using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
解决方法
1. 手动下载并配置EdgeDriver
- 步骤1:确认Edge版本:打开Edge浏览器,进入「设置和更多」→「设置」→「关于Microsoft Edge」,查看浏览器的版本号(例如119.0.2151.58)。
- 步骤2:下载对应版本的EdgeDriver:前往微软官方EdgeDriver下载页面,选择与浏览器版本完全匹配、对应系统(Windows 64位)的驱动包,下载后解压得到
msedgedriver.exe。 - 步骤3:配置驱动路径:
- 方法一:将
msedgedriver.exe移动到Python安装目录的Scripts文件夹下(例如C:\Python311\Scripts),或添加到系统环境变量PATH中。 - 方法二:在代码中直接指定驱动路径:
from selenium import webdriver from selenium.webdriver.edge.service import Service # 替换为你的msedgedriver.exe实际路径 driver_path = r"C:\Users\Queensley\Desktop\msedgedriver.exe" service = Service(driver_path) browser = webdriver.Edge(service=service)
- 方法一:将
2. 排查网络问题
- 检查网络是否能正常访问EdgeDriver的下载服务器,若处于受限制的网络环境(如公司内网),尝试切换网络(比如使用手机热点)。
- 若使用代理,需在代码中配置代理参数,确保Selenium Manager能正常连接服务器。
内容的提问来源于stack exchange,提问作者Kobbyprince
相关产品推荐
相关产品推荐

