树莓派导入Selenium Webdriver报错的原因及解决咨询
问题描述
在Raspbian GNU/Linux 11(bullseye,debian_version 11.8)的树莓派上,使用venv创建的Python3.9.2环境,通过以下步骤配置Selenium:
- 用
sudo apt install rpi-chromium-mods安装Chromium - 执行
python3 -m pip install selenium安装Selenium - 下载并设置
chromedriver权限,放置在指定目录,同时通过sudo apt-get install chromium-chromedriver安装系统级chromedriver - 将chromedriver路径添加到
.bashrc的PATH中
曾尝试用conda替代venv,导致Python降级到3.4且无法升级,卸载conda后仍出现导入报错:
Python 3.9.2 (default, Mar 12 2021, 04:06:34) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import selenium >>> from selenium import webdriver Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/myapp/.myapp/lib/python3.9/site-packages/selenium/webdriver/__init__.py", line 20, in <module> from .chrome.webdriver import WebDriver as Chrome # noqa File "/home/pi/myapp/.myapp/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 18, in <module> from selenium.webdriver.chromium.webdriver import ChromiumDriver File "/home/pi/myapp/.myapp/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 22, in <module> from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver File "/home/pi/myapp/.myapp/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 57, in <module> from ..common.fedcm.dialog import Dialog ModuleNotFoundError: No module named 'selenium.webdriver.common.fedcm'
即使将Selenium的site-packages路径添加到系统路径,错误仍存在。更换Ubuntu系统后,安装最新Selenium仍出现浏览器崩溃、架构不支持错误,最终安装selenium==4.26.1后,以下代码可正常运行:
import platform from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager options = Options() # a few usefull options options.add_argument("--disable-infobars") options.add_argument("start-maximized") options.add_argument("--disable-extensions") options.add_argument("--headless") # if you want it headless if platform.system() == "Linux" and platform.machine() == "armv7l": # if raspi options.BinaryLocation = ("/usr/bin/chromium-browser") service = Service("/usr/bin/chromedriver") else: # if not raspi and considering you're using Chrome service = Service(ChromeDriverManager().install()) driver = webdriver.Chrome( service=service, options=options ) url = 'https://stackoverflow.com/questions/79264531/why-i-get-an-error-while-importing-selenium-webdriver' driver.get(url) print(driver.title) #finish driver.quit()
咨询:
- 该导入报错是路径问题、安装失败还是环境问题?
- 为何
import selenium成功但from selenium import webdriver失败? - 是否有其他解决建议?
问题分析与解答
1. 导入报错的根源
这是环境兼容性问题,具体是Selenium版本与Python3.9的兼容性冲突。最新版Selenium(4.27及以上)已不再支持Python3.9,selenium.webdriver.common.fedcm是高版本Selenium新增的模块,但由于Python3.9已不在官方支持列表中,安装过程中可能出现模块缺失,或者Python3.9无法正确解析该模块的导入逻辑,导致报错。
2. 为什么import selenium成功但from selenium import webdriver失败
import selenium仅加载Selenium的顶层包(仅执行顶层__init__.py中的内容),不需要加载深层子模块;而from selenium import webdriver会触发加载webdriver相关的一系列嵌套子模块,其中依赖的fedcm模块要么未被正确安装,要么当前Python版本无法兼容该模块的导入规则,因此触发报错。
3. 其他解决建议
- 锁定兼容的Selenium版本:继续使用
selenium==4.26.1,这是最后一批支持Python3.9的稳定版本,也是已验证有效的方案。 - 清理残留的Selenium安装:执行
pip uninstall selenium -y彻底卸载虚拟环境中的Selenium,同时删除系统中可能存在的旧版本Selenium文件(如apt安装的),避免版本冲突。 - 避免混用包管理工具:不要同时用apt和pip安装Selenium,坚持在venv虚拟环境中用pip单独安装,确保环境依赖隔离。
- 验证浏览器与驱动版本匹配:用
chromium-browser --version和chromedriver --version检查版本,两者版本差异不能超过3个大版本,否则会导致浏览器启动失败。 - 放弃conda管理树莓派Python环境:树莓派armv7l架构下,conda对Python版本的支持不完善,容易出现版本降级问题,坚持使用venv即可。
内容的提问来源于stack exchange,提问作者thermike
相关产品推荐
相关产品推荐

