undetected_chromedriver与webdriver_manager执行格式错误求助
解决undetected_chromedriver搭配webdriver_manager出现的Exec format error问题
问题现象
OSError: [Errno 8] Exec format error: '/Users/pd/.wdm/drivers/chromedriver/mac64/127.0.6533.72/chromedriver-mac-x64/THIRD_PARTY_NOTICES.chromedriver'
问题根源
webdriver_manager近期更新后,ChromeDriverManager().install()返回的路径错误指向了目录中的第三方通知文件,而非真正的chromedriver可执行程序,导致执行时出现格式不兼容错误。
解决方案
方法一:手动修正驱动路径
mac平台下chromedriver的实际可执行文件位于下载目录的chromedriver-mac-x64/chromedriver路径,通过路径拼接获取正确地址:
import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.support import expected_conditions as EC import os def get_driver(): options = uc.ChromeOptions() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-sim-usage") options.add_argument("--start-maximized") options.add_argument('--disable-popup-blocking') # 获取webdriver_manager下载的基础路径 base_path = ChromeDriverManager().install() # 拼接得到真正的chromedriver可执行文件路径 correct_driver_path = os.path.join(os.path.dirname(base_path), "chromedriver") driver = uc.Chrome(driver_executable_path=correct_driver_path, options=options, version_main=116) driver.maximize_window() return driver
方法二:降级webdriver_manager版本
回滚到之前验证可用的版本,比如4.0.1版本:
pip install webdriver-manager==4.0.1
执行后保留原有代码即可恢复正常。
方法三:使用undetected_chromedriver自带驱动管理
undetected_chromedriver本身具备自动下载、匹配对应版本驱动的能力,无需依赖webdriver_manager,稳定性更强:
import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def get_driver(): options = uc.ChromeOptions() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-sim-usage") options.add_argument("--start-maximized") options.add_argument('--disable-popup-blocking') # 无需指定driver_executable_path,uc自动处理驱动下载与匹配 driver = uc.Chrome(options=options, version_main=116) driver.maximize_window() return driver
内容的提问来源于stack exchange,提问作者Prakash Dahal
相关产品推荐
相关产品推荐

