我在 Mac(ARM64/M3 芯片)上的 Python Selenium ChromeDriverManager 中遇到了问题。
当我运行以下 Python 代码时,出现以下错误:
[Errno 8] Exec format error: '/Users/adam.westbrook/.wdm/drivers/chromedriver/mac64/129.0.6668.89/chromedriver-mac-arm64/THIRD_PARTY_NOTICES.chromedriver'
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))
出于某种原因,ChromeDriverManager 尝试使用
THIRD_PARTY_NOTICES.chromedriver
作为驱动程序而不是文件 chromedriver
。
当我查看该文件夹时,文件的文件类型
chromedriver
显示它是一个文档而不是 Unix 可执行文件。
但是当我手动解压自动下载的zip文件时,
chromedriver
然后正确显示它是一个Unix可执行文件。
我该如何解决这个问题?
ChatGPT 提供的这个解决方案解决了我的问题。
它本质上需要将路径从
THIRD_PARTY_NOTICES.chromedriver
重新映射到 chromedriver
。然后设置它,使其成为可执行文件。
这个解决方案对我有用!
import os
import stat
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
# Use webdriver_manager to install ChromeDriver
driver_path = ChromeDriverManager().install()
driver_path = driver_path.replace('THIRD_PARTY_NOTICES.chromedriver', 'chromedriver')
# Ensure the downloaded chromedriver has executable permissions
# Check if it’s a valid file and set the executable bit
# i.e. explicitly sets the file’s permissions to be readable, writable, and executable by the user (you). This ensures that macOS recognizes it as an executable binary.
if os.path.exists(driver_path):
os.chmod(driver_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
# Use the installed ChromeDriver for Selenium
driver = webdriver.Chrome(service=ChromeService(executable_path=driver_path))