【问题标题】:Selenium with chromedriver doesn't start via cron带有 chromedriver 的 Selenium 不能通过 cron 启动
【发布时间】:2020-10-03 12:54:02
【问题描述】:

在 CentOS7 上以 headless 模式 使用 Selenium 和 Chromedriver 的 Python 脚本在手动调用时运行良好。

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('no-sandbox')
self.driver = webdriver.Chrome(chrome_options=options)

当使用 crontab 启动脚本时,它会在第 4 行(上图)抛出此异常。底部的完整回溯。

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.38.552522

使用 crontab -e 设置 Cron

* * * * * cd /to/path && /to/path/.virtualenvs/selenium/bin/python /to/path/script.py -t arg1 arg2 > /to/path/log.txt 2>&1

这会产生无法找到 chromedriver 之类的错误。然后我在 crontab -e 中添加了以下内容。
1) 使用 bash 而不是 sh,虽然从 sh 手动启动 python 脚本工作正常
2) 指定chromedriver的路径

SHELL=/bin/bash
PATH=/usr/local/bin/

我尝试了在网上找到的不同建议,例如在我的脚本中向 chromedriver 添加 --no-sandbox 选项。一切都没有帮助。请注意,我在无头模式下使用 chrome,所以我认为我不需要像以前那样在 cron 或 Xvfb 库中使用 export DISPLAY=:0 的东西。

Python 3.6.1
硒 3.4.3
Chromedriver 2.38.552522
google-chrome-stable 65.0.3325.181

完整的追溯

Exception in thread <name>:
Traceback (most recent call last):
  File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/path/to/script.py", line 53, in start
    self.site_scrape(test_run)
  File "/path/to/script.py", line 65, in site
    self.driver = webdriver.Chrome(chrome_options=options)
  File "/home/<user>/.virtualenvs/selenium/lib64/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/<user>/.virtualenvs/selenium/lib64/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/home/<user>/.virtualenvs/selenium/lib64/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/<user>/.virtualenvs/selenium/lib64/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/home/<user>/.virtualenvs/selenium/lib64/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Linux 4.14.12-x86_64-linode92 x86_64)

【问题讨论】:

  • 您可以尝试通过crontab -u 指定用户并确保您的chromedriver bin 具有适当的可执行权限吗?
  • crontab -u &lt;user&gt; -l 显示正确的 cron 条目。 chromedriver 有以下权限-rwxr-xr-x 1 root root 7872560 Mar 2 02:19 chromedriver
  • 是的,看起来差不多。我最后的建议是尝试2.37 和2.38 chromedrivers,因为它们都支持你的浏览器版本。
  • 谢谢。但仍然有同样的错误selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.38.552522
  • 使用错误堆栈跟踪更新问题

标签: python selenium cron selenium-chromedriver centos7


【解决方案1】:

终于找到了解决办法。男孩,这让我困扰太久了。问题在于缺少 PATH 目录:cron 中的 /usr/bin、/usr/sbin。完整的 cron 现在看起来像这样:

SHELL=/bin/bash
PATH=/usr/local/bin/:/usr/bin:/usr/sbin
* * * * * cd /to/path && /to/path/.virtualenvs/selenium/bin/python /to/path/script.py -t arg1 arg2 > /to/path/log.txt 2>&1

【讨论】:

  • 应该注意,由于这会覆盖PATH变量,它可能会在其他地方造成严重破坏。
  • 非常感谢。我有同样的问题...我在 shell 上运行 echo $PATH 并将结果放在 crontab 内的 PATH 中,它解决了问题。
  • 只想补充一点(显然我猜,因为问题与 python 或 chromedriver 无关)也适用于 ruby​​ 和 geckodriver。我找到了这个答案,所以其他人可能也会,但也许值得更新标签?
【解决方案2】:

以下步骤对我有帮助:

  1. 将 'DISPLAY=:1' 添加到我的 crontab 中
  2. 在 crontab 中设置正确的 shell(我使用 'zsh')
  3. 获取环境变量,因为 crontab 默认情况下不这样做
  4. 使用 chromedriver 的绝对路径

TLDR

  1. 需要对您的 crontab 进行修改:

    SHELL=/bin/zsh
    05 * * * * export DISPLAY=:<displayNumber> && source /home/<username>/.zshrc && cd <absoluteExecutableDirectory> && ./<pythonFile> >> log.log 2>&1
    
  2. 使用下面这行来初始化 selenium chrome 驱动:

    driver = webdriver.Chrome(<absoluteDriverPath>,...)
    

用各自的值替换尖括号内的所有内容。

1。设置显示

要找出要添加到 crontab 的显示,请使用:

    env | grep 'DISPLAY'

然后将此片段添加到您的 crontab 命令中:

    export DISPLAY=:1

2。设置外壳

  • 如果您有非默认 shell*,请设置 shell。 使用两个命令之一找出你的 shell 的位置

    which bash
    which zsh
    

    然后将 shell 设置为上一个命令的响应(在您的 crontab 中):

    SHELL=/bin/zsh
    

3。获取环境变量

将以下内容之一添加到您的 crontab 命令中,具体取决于您使用 bash 还是 zsh:

    source /home/<username>/.zshrc
    source /home/<username>/.bashrc

4。为您的 chromedriver 使用绝对路径:

在初始化驱动程序时,使用以下指向 selenium chrome 驱动程序的行。

    driver = webdriver.Chrome(<absoluteDriverPath>,options=options)

其他

    >> log.log 2>&1

意味着将所有输出写入文件(这样可以更轻松地调试 crontab)。

【讨论】:

  • 您在此处指向什么可执行文件:&amp;&amp; cd &lt;absoluteExecutableDirectory&gt; &amp;&amp;?谢谢
【解决方案3】:

在 ubuntu 18.04、Python 3.6.9 上:

它在我的主工作站上,所以我总是有一个登录的 X 会话。

我的硒调用:

#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
location = 'http://example.com'
driver.get(location)

我在 crontab 中放了什么:

33 14 * * * DISPLAY=:0 lxterminal --working-directory=/home/user/Documents/ -e /home/user/bin/get.stats.by.zip.py > /home/user/gbzp.log 2>&1

lxterminal 是一个相对简单的终端程序,我倾向于将它用于大多数终端程序,而不是使用 apt-get 安装的 gnome-terminal。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多