Selenium/WebDriver无法运行问题求助(Windows10+Selenium4.1.3)
Selenium 4.1.3在Windows 10下报错WinError 193的问题分析
环境与问题背景
- 系统:Windows 10
- Selenium版本:4.1.3
- 问题:一周前代码突然报错,已尝试更新Selenium和ChromeDriver,问题仍未解决
运行代码
from selenium import webdriver from selenium.webdriver import Chrome from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument('--disable-dev-shm-usage') chrome_driver = ChromeDriverManager().install() driver = Chrome(service=Service(chrome_driver), chrome_options=chrome_options)
错误信息
c:\Users\User\Desktop\text.py:14: DeprecationWarning: use options instead of chrome_options driver = Chrome(service=Service(chrome_driver), chrome_options=chrome_options) Traceback (most recent call last): File "c:\Users\User\Desktop\text.py", line 14, in <module> driver = Chrome(service=Service(chrome_driver), chrome_options=chrome_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in __init__ self.service.start() File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\service.py", line 71, in start self.process = subprocess.Popen(cmd, env=self.env, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1026, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1538, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OSError: [WinError 193] %1 不是有效的 Win32 应用程序
问题原因与解决方法
核心错误原因
WinError 193本质是尝试执行的文件不是有效的Win32程序,对应到当前场景,大概率是以下情况之一:
ChromeDriverManager下载的ChromeDriver文件损坏- 下载的ChromeDriver版本与本地Chrome浏览器版本不兼容
- 下载的驱动位数(32位/64位)与Windows 10系统不匹配
次要问题:参数废弃警告
代码中使用的chrome_options参数在Selenium 4.x版本已被废弃,需改用options参数,该警告虽不会直接导致崩溃,但建议修正以避免后续潜在问题。
具体解决步骤
手动匹配ChromeDriver版本
- 打开Chrome浏览器,输入
chrome://version/查看当前浏览器版本 - 下载与Chrome版本完全一致的ChromeDriver,选择对应Windows系统的位数
- 找到
ChromeDriverManager().install()返回的驱动路径,替换为手动下载的chromedriver.exe
- 打开Chrome浏览器,输入
修正废弃参数
将代码最后一行修改为:driver = Chrome(service=Service(chrome_driver), options=chrome_options)清理驱动缓存
删除webdriver_manager的缓存目录(通常路径为C:\Users\[你的用户名]\.wdm),重新运行代码让工具重新下载正确驱动
内容的提问来源于stack exchange,提问作者Luca
相关产品推荐
相关产品推荐

