树莓派3B+通过crontab启动Selenium脚本异常退出求助
树莓派3B+通过crontab运行Selenium Python脚本失败问题解决
问题背景
在树莓派3B+上,设备通过HDMI连接屏幕,位于/home/pi/Documents/目录下的Selenium Python脚本在终端中可正常运行,但通过crontab执行时失败。
脚本代码(test2.py)
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service chrome_options = Options() chrome_options.BinaryLocation = "/usr/lib/chromium-browser" driver_path = "/usr/lib/chromium-browser/chromedriver" chrome_options.add_argument('disable-infobars') chrome_options.add_argument("--kiosk") chrome_options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(service=Service(driver_path), options=chrome_options) driver.get("https://www.google.com")
当前crontab配置
* * * * * /usr/bin/python3 /home/pi/Documents/test2.py > /home/pi/Documents/logs.txt 2>&1
错误日志(logs.txt)
Traceback (most recent call last): File "/home/pi/Documents/test2.py", line 11, in <module> driver = webdriver.Chrome(service=Service(driver_path), options=chrome_options) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__ super().__init__( File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__ super().__init__( File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__ self.start_session(capabilities, browser_profile) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
已尝试的调试方法
- 将代码中的驱动路径替换为
/usr/bin/chromiumdriver - 将代码中的BinaryLocation替换为
/usr/bin/chromium-browser - 在crontab中添加
DISPLAY=:0 - 在crontab中添加自定义PATH
- 在crontab中添加终端
echo $PATH的返回值作为PATH - 重新安装selenium
- 安装webdriver-manager
解决方案
1. 修正Chrome启动参数
编辑test2.py,补充必要的Chrome选项解决DevToolsActivePort和权限问题:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service chrome_options = Options() # 树莓派上Chromium的正确路径通常为/usr/bin/chromium-browser chrome_options.BinaryLocation = "/usr/bin/chromium-browser" driver_path = "/usr/bin/chromedriver" # 对应chromedriver的正确路径 # 添加关键参数 chrome_options.add_argument('disable-infobars') chrome_options.add_argument("--kiosk") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--no-sandbox") # 禁用沙箱,解决crontab环境下权限限制 chrome_options.add_argument("--display=:0") # 指定显示端口 chrome_options.add_argument("--user-data-dir=/home/pi/.config/chromium") # 指定用户数据目录,避免会话冲突 driver = webdriver.Chrome(service=Service(driver_path), options=chrome_options) driver.get("https://www.google.com")
2. 完善crontab环境配置
执行crontab -e编辑定时任务,添加XAUTHORITY环境变量确保crontab能访问X会话:
* * * * * DISPLAY=:0 XAUTHORITY=/home/pi/.Xauthority /usr/bin/python3 /home/pi/Documents/test2.py > /home/pi/Documents/logs.txt 2>&1
XAUTHORITY指向当前用户的X认证文件,解决crontab无X会话权限的问题
3. 验证Chromium与ChromeDriver版本匹配
执行以下命令检查版本:
chromium-browser --version chromedriver --version
确保两者主版本号一致,若不匹配,重新下载对应版本的chromedriver替换现有文件。
4. 检查脚本权限
确保脚本具备执行权限:
chmod +x /home/pi/Documents/test2.py
内容的提问来源于stack exchange,提问作者Jack Bow
相关产品推荐
相关产品推荐

