通过Cron从Python运行Selenium和Chromedriver

-1 投票
1 回答
56 浏览
提问于 2025-04-14 17:16

我在树莓派5上运行着 SeleniumChromedriver 和 Python。因为 Chromedriver 没有专门为 ARM 提供的下载版本,但在 APT 上有一个版本可以用。

https://forums.raspberrypi.com/viewtopic.php?p=2155925#p2155925

所以,我一直在用这个,运行起来很顺利,特别是在 Geany 这个环境下。

现在我已经完成了所有工作,想通过 CRON 来运行这个 Python 脚本,所以我把相关信息填进去,但出现了错误。

Traceback (most recent call last):   File "/home/dean/main_venv/luxpowerdl.py", line 69, in <module>
    driver = webdriver.Chrome(service=service)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 61, in __init__
    super().__init__(command_executor=executor, options=options)   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
    self.start_session(capabilities)   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)   File "/home/dean/main_venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.   (session not created: 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.) Stacktrace:
#0 0x5555f50dc3e4 <unknown>
#1 0x5555f4e11ac8 <unknown>
#2 0x5555f4e40fac <unknown>
#3 0x5555f4e3dc44 <unknown>
#4 0x5555f4e7cf08 <unknown>
#5 0x5555f4e48d74 <unknown>
#6 0x5555f4e49760 <unknown>
#7 0x5555f50a6a74 <unknown>
#8 0x5555f50a9e04 <unknown>
#9 0x5555f5096d7c <unknown>
#10 0x5555f50aa714 <unknown>
#11 0x5555f507f0e0 <unknown>
#12 0x5555f50cbbc4 <unknown>
#13 0x5555f50cbdc0 <unknown>
#14 0x5555f50db5b4 <unknown>
#15 0x7fff46fcee58 <unknown>
#16 0x7fff47037f9c <unknown>

APT 安装 Chromedriver 的地方是 usr/bin,文件的拥有者是 Root,权限设置得也不错。

usr/bin 在我的 $PATH 里。

所以,Python 文件在 Geany、我的用户终端中运行得很好,我的用户名在 Root 组里,Chromedriver 的拥有者是 root,权限是 rwx(拥有者可读写执行),组用户是 r-x(可读执行),其他用户也是 r-x(可读执行),而且它也被标记为可执行文件。

有没有什么办法让 cron(使用 crontab -e)能够访问到 chromedriver 呢?

顺便说一下,Chromium 浏览器已经安装了。所有操作都是在运行 Debian Bookworm 的树莓派5上进行的。

1 个回答

0

我终于找到了一条关于在我的定时任务(cron)中使用 DISPLAY=:0 的参考资料。我原本以为这根本不可能,因为它需要 X 窗口系统在运行,但 Debian Bookworm 是用 Wayland 的。不过,我还是试了一下,结果运行得非常顺利。

所以我现在的定时任务设置是:

*/15 * * * * DISPLAY=:0 /home/dean/main_venv/bin/python main_venv/luxpowerdl.py > main/venv/logs/luxpowerdl.log 2>&1

撰写回答