You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署discord.py+Selenium机器人时遭遇NoSuchDriverException求助

问题:Discord机器人部署服务器后Selenium报NoSuchDriverException

本地运行正常,部署到服务器后出现NoSuchDriverException,已通过requirements.txt安装所有依赖。


代码片段

import asyncio
import functools
import time
import typing
from converters import *

from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
import re


def scrap():
    url = 'https://www.cryptocraft.com/calendar?week=this'

    op = Options()

    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'

    op.add_argument("--no-sandbox")
    op.add_argument("--headless")
    op.add_argument("start-maximized")
    op.add_argument("window-size=1900,1080")
    op.add_argument("disable-gpu")
    op.add_argument("--disable-software-rasterizer")
    op.add_argument("--disable-dev-shm-usage")
    op.add_argument(f'user-agent={user_agent}')
    op.add_argument("disable-infobars")
    op.add_argument("--disable-extensions")
    op.add_argument('--disable-application-cache')

    driver = webdriver.Chrome(options=op)
    
    # 后续爬取逻辑...

报错日志

File "/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 126, in run
    completed_proc = subprocess.run(args, capture_output=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.11/subprocess.py", line 1950, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/common/linux/selenium-manager'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/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 "/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 95, in driver_location
    output = self.run(args)
             ^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 132, in run
    raise WebDriverException(f"Unsuccessful command executed: {command}") from err
selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: /home/container/.local/lib/python3.11/site-packages/selenium/webdriver/common/linux/selenium-manager --browser chrome --output json
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/tasks/__init__.py", line 239, in _loop
    await self.coro(*args, **kwargs)
  File "/home/container/app.py", line 47, in send_news
    res = await run_blocking(scrap)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/app.py", line 30, in run_blocking
    return await client.loop.run_in_executor(None, func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/scraper.py", line 37, in scrap
    driver = webdriver.Chrome(options=op)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/container/.local/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/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 chrome using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

requirements.txt内容

selenium==4.12.0 discord==2.3.2 pytz==2023.3

解决方案

从日志里的OSError: [Errno 8] Exec format error可以看出,核心问题是Selenium Manager二进制文件与服务器架构不兼容,或服务器未安装Chrome浏览器。以下是针对性解决步骤:

1. 检查服务器架构

如果服务器是ARM架构(如AWS Graviton、树莓派),Selenium 4.12.0的Selenium Manager可能未适配ARM版本。可直接升级到最新版Selenium,或手动安装ChromeDriver。

2. 手动安装Chrome与ChromeDriver(Debian/Ubuntu为例)

# 更新源并安装Chrome浏览器和对应驱动
sudo apt update
sudo apt install -y chromium-browser chromium-chromedriver

修改代码中driver初始化逻辑,指定ChromeDriver路径:

from selenium.webdriver.chrome.service import Service

# ...其他代码...

# 替换为服务器上chromedriver的实际路径
service = Service('/usr/lib/chromium-browser/chromedriver')
driver = webdriver.Chrome(service=service, options=op)

3. 优化ChromeOptions参数

保留必要参数,替换旧版无头模式参数为新版:

op.add_argument("--no-sandbox")
op.add_argument("--headless=new")  # 新版Chrome推荐使用此参数
op.add_argument("--disable-dev-shm-usage")
op.add_argument(f'user-agent={user_agent}')
# 保留其他业务必需参数

4. 升级Selenium版本

修改requirements.txt中的Selenium版本为最新稳定版:

selenium==4.21.0 discord==2.3.2 pytz==2023.3

重新安装依赖:

pip install -r requirements.txt

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:40:54