You need to enable JavaScript to run this app. 树莓派3B+上Selenium脚本手动运行正常,crontab/LXDE自启时仅启动浏览器未执行后续逻辑
优惠活动
大模型
产品
解决方案
定价
更多

树莓派3B+上Selenium脚本手动运行正常,crontab/LXDE自启时仅启动浏览器未执行后续逻辑

树莓派3B+ Selenium脚本自启运行异常

问题描述

  • 手动执行命令/usr/bin/python /home/pi/main.py时,脚本可完成全部预设的页面交互操作,运行状态正常
  • 通过crontab、LXDE-pi/autostart配置自启动运行时,仅能成功拉起Chrome浏览器窗口,无法执行后续的元素定位、表单输入逻辑
  • 已通过Python logging库捕获运行日志,但日志仅记录到Chrome会话初始化完成节点,无有效故障定位信息
  • 已排除依赖权限问题:即使用sudo权限手动启动Python脚本,运行状态与普通用户执行完全一致,不存在权限导致的功能异常
  • 当前配置的crontab自启规则为:
@reboot python /home/pi/main.py

相关代码

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import logging

logging.basicConfig(level=logging.DEBUG)
# 配置Chrome kiosk模式,关闭自动化测试提示弹窗
chromeOptions = Options()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument("--kiosk")
chromeOptions.add_experimental_option("excludeSwitches", ["enable-automation"])
chromeOptions.add_experimental_option('useAutomationExtension', False)
# 关闭密码保存提示弹窗
chromeOptions.add_experimental_option('prefs', {
    'credentials_enable_service': False,
    'profile': {
        'password_manager_enabled': False
    }
})

# 按配置初始化Chrome驱动
driver = webdriver.Chrome(chrome_options=chromeOptions)
# 设置隐式等待时长为30秒,默认值为0
driver.implicitly_wait(30)
# 跳转至目标URL
driver.get(url)
# 定位登录页邮箱输入框
mail_name = "loginfmt"
# 等待邮箱元素可交互
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.NAME, mail_name)))

mail_form = driver.find_element_by_name(mail_name)
# 输入账号后按回车提交
mail_form.send_keys(usr)
mail_form.send_keys(Keys.RETURN)

# 定位登录页密码输入框
pass_name = "passwd"
# 等待密码元素可交互
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.NAME, pass_name)))
pass_form = driver.find_element_by_name(pass_name)
# 输入密码后按回车提交
pass_form.send_keys(passwd)
pass_form.send_keys(Keys.RETURN)

自启运行日志

DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:38727/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:38727 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:35557/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:35557 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:34903/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:34903 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:40785/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:40785 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

问题根因

该故障本质是自启调度场景下的运行环境与手动登录桌面的运行环境存在差异,并非Selenium本身或文件权限问题:

  • crontab的@reboot任务在系统启动早期即被调度,此时LXDE图形桌面服务尚未完成初始化,DISPLAY环境变量未正确加载。Chrome作为依赖图形界面的应用虽能拉起进程,但无法正常完成页面渲染与DOM交互,后续元素定位、输入操作会静默卡住,不会触发报错日志
  • 自启调度阶段网络服务未完全就绪,会导致driver.get(url)请求失败,后续的WebDriverWait等待将永远无法满足触发条件,程序表现为仅打开Chrome窗口无后续操作

修复方案

  • 修正crontab自启配置,补充图形界面所需环境变量,增加启动延迟等待系统服务就绪:
    @reboot sleep 30 && DISPLAY=:0 /usr/bin/python /home/pi/main.py >> /home/pi/selenium_run.log 2>&1
    
    配置逻辑说明:
    • 增加30秒启动延迟,保证系统启动后网络、桌面服务完全加载完成
    • 显式指定DISPLAY=:0环境变量,让Chrome关联到本地LXDE的图形渲染窗口
    • 将标准输出、标准错误重定向到日志文件,捕获之前遗漏的程序报错信息
  • 若使用LXDE-pi/autostart自启,可直接在配置文件中添加启动项:
    @/usr/bin/python /home/pi/main.py
    
    该方式运行环境与手动登录桌面的环境完全一致,不存在DISPLAY环境变量缺失问题
  • 修正Selenium代码兼容问题:新版Selenium中chrome_options参数已废弃,需替换为options参数,避免驱动初始化时出现兼容告警

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.05 10:27:06