Microsoft Edge Driver启动失败求助(Google Colab环境)
问题:Google Colab中Edge Driver启动失败(WebDriverException)
在Google Colab运行Selenium Edge自动化代码时,启动Edge Driver失败,抛出WebDriverException,核心错误提示:
unknown error: Microsoft Edge failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from msedge location /usr/bin/microsoft-edge is no longer running, so msedgedriver is assuming that msedge has crashed.)
已完成的排查步骤:
- EdgeDriver与Microsoft Edge版本完全匹配
- WebDriver路径配置正确
- Selenium已更新至最新版本
- 执行
chmod +x msedgedriver赋予执行权限
原运行代码
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.edge.service import Service # Prompt the user for Microsoft Edge browser sign-in credentials username = input("Enter your username: ") password = input("Enter your password: ") # Set the path to the Microsoft Edge WebDriver executable webdriver_path = "/usr/local/bin/microsoft-edgedriver.exe" # Replace with the actual path # Create a new instance of the Service object # service = webdriver.Service(webdriver_path) # Start the service # service.start() edge_options = webdriver.EdgeOptions() # Create a new instance of the Microsoft Edge driver driver = webdriver.Edge(executable_path=webdriver_path, options=edge_options) # Maximize the browser window (optional) driver.maximize_window() # Navigate to the Microsoft sign-in page driver.get("https://login.live.com/") # Wait until the username field is visible username_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME, "loginfmt")) ) # Enter the username username_field.send_keys(username) username_field.send_keys(Keys.RETURN) # Wait until the password field is visible password_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME, "passwd")) ) # Enter the password password_field.send_keys(password) password_field.send_keys(Keys.RETURN) # Wait until the search box is visible search_box = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME, "q")) ) # Enter the search query search_box.send_keys("hello") search_box.send_keys(Keys.RETURN) # Close the browser driver.quit()
完整错误栈
WebDriverException Traceback (most recent call last) <ipython-input-24-4739c578a60b> in <cell line: 30>() 28 # driver = webdriver.Edge(service=service) 29 # driver = webdriver.Edge(service=Service(EdgeDriverManager().install())) ---> 30 driver = webdriver.Edge(executable_path=webdriver_path, options=edge_options) 31 32 # Maximize the browser window (optional) 5 frames /usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response) 243 alert_text = value["alert"].get("text") 244 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here --> 245 raise exception_class(message, screen, stacktrace) WebDriverException: Message: unknown error: Microsoft Edge failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from msedge location /usr/bin/microsoft-edge is no longer running, so msedgedriver is assuming that msedge has crashed.) Stacktrace: #0 0x5586b70e7f93 <unknown> #1 0x5586b6df48c1 <unknown> #2 0x5586b6e20cdf <unknown> #3 0x5586b6e1c1ed <unknown> #4 0x5586b6e5cea5 <unknown> #5 0x5586b6e54043 <unknown> #6 0x5586b6e27941 <unknown> #7 0x5586b6e28b5e <unknown> #8 0x5586b70b4536 <unknown> #9 0x5586b70b8049 <unknown> #10 0x5586b70b7b39 <unknown> #11 0x5586b70b8505 <unknown> #12 0x5586b70bf9db <unknown> #13 0x5586b70b88ae <unknown> #14 0x5586b709218c <unknown> #15 0x5586b70d3d08 <unknown> #16 0x5586b70d3e44 <unknown> #17 0x5586b70e1766 <unknown> #18 0x7fefa654d609 start_thread
解决方案
问题核心是Google Colab的Linux无头环境缺少必要的浏览器启动参数,需修改edge_options配置,添加以下关键参数:
- 启用无头模式:Colab无图形界面,必须以无头模式启动浏览器
- 禁用沙箱:避免Linux环境下的权限限制导致启动失败
- 指定DevTools端口与临时路径:解决
DevToolsActivePort文件不存在的问题 - 禁用GPU与设置窗口大小:适配Colab环境的资源限制
修改后的完整代码
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.edge.service import Service import tempfile # Prompt the user for credentials username = input("Enter your username: ") password = input("Enter your password: ") # WebDriver路径(注意Colab中Linux环境无需.exe后缀) webdriver_path = "/usr/local/bin/microsoft-edgedriver" edge_options = webdriver.EdgeOptions() # 核心配置:适配Colab无头环境 edge_options.add_argument("--headless=new") # 新版无头模式,兼容性更好 edge_options.add_argument("--no-sandbox") # 禁用沙箱,解决权限问题 edge_options.add_argument("--disable-dev-shm-usage") # 避免共享内存不足 edge_options.add_argument("--remote-debugging-port=9222") # 指定DevTools端口 edge_options.add_argument("--window-size=1920,1080") # 设置窗口大小 edge_options.add_argument("--disable-gpu") # 禁用GPU,适配无头环境 # 创建临时目录存放DevTools相关文件 temp_dir = tempfile.mkdtemp() edge_options.add_argument(f"--user-data-dir={temp_dir}") edge_options.add_argument(f"--data-path={temp_dir}") edge_options.add_argument(f"--disk-cache-dir={temp_dir}") # 初始化Driver(建议使用Service类,Selenium 4+推荐写法) service = Service(webdriver_path) driver = webdriver.Edge(service=service, options=edge_options) try: # 执行自动化流程 driver.get("https://login.live.com/") # 等待并输入用户名 username_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME, "loginfmt")) ) username_field.send_keys(username) username_field.send_keys(Keys.RETURN) # 等待并输入密码 password_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME, "passwd")) ) password_field.send_keys(password) password_field.send_keys(Keys.RETURN) # 等待搜索框并执行搜索 search_box = WebDriverWait(driver, 15).until( EC.visibility_of_element_located((By.NAME, "q")) ) search_box.send_keys("hello") search_box.send_keys(Keys.RETURN) finally: # 确保浏览器关闭 driver.quit()
额外注意事项
- 移除WebDriver路径中的
.exe后缀:Colab是Linux环境,可执行文件无需Windows后缀 - 使用
Service类初始化Driver:Selenium 4及以上版本推荐使用Service替代直接指定executable_path - 添加
try-finally块:保证无论是否出错都能关闭浏览器,避免资源泄漏
内容的提问来源于stack exchange,提问作者ALEX
相关产品推荐
相关产品推荐

