Jim*_*m B 9 python cron selenium selenium-chromedriver centos7
在 CentOS7 上以无头模式使用 Selenium 和 Chromedriver 的 Python 脚本在手动调用时运行良好。
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('no-sandbox')
self.driver = webdriver.Chrome(chrome_options=options)
Run Code Online (Sandbox Code Playgroud)
然而,当用 crontab 启动脚本时,它会在第 4 行(上面)抛出这个异常。底部的完整追溯。
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.38.552522
Run Code Online (Sandbox Code Playgroud)
Cron 是用 crontab -e 设置的
* * * * * cd /to/path && /to/path/.virtualenvs/selenium/bin/python /to/path/script.py -t arg1 arg2 > /to/path/log.txt 2>&1
Run Code Online (Sandbox Code Playgroud)
这产生了找不到 chromedriver 之类的错误。然后我在 crontab -e 中添加了以下内容。
1) 使用 bash 而不是 sh,虽然从 sh 手动启动 python 脚本工作正常
2) 指定 chromedriver 的路径
SHELL=/bin/bash
PATH=/usr/local/bin/
Run Code Online (Sandbox Code Playgroud)
我尝试了在网络上找到的不同建议,例如在我的脚本中向 chromedriver 添加 --no-sandbox 选项。一切都没有帮助。请注意,我在无头模式下使用 chrome,所以我认为我不需要像以前那样在 cron 或 Xvfb 库中使用这个 export DISPLAY=:0 东西。
Python 3.6.1
Selenium 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)
Run Code Online (Sandbox Code Playgroud)
终于找到了解决办法。男孩,这困扰我太久了。问题在于缺少 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
Run Code Online (Sandbox Code Playgroud)
小智 5
对我有帮助的是以下步骤:
对 crontab 所需的修改是:
SHELL=/bin/zsh
05 * * * * export DISPLAY=:<displayNumber> && source /home/<username>/.zshrc && cd <absoluteExecutableDirectory> && ./<pythonFile> >> log.log 2>&1
Run Code Online (Sandbox Code Playgroud)使用以下行初始化 selenium chrome 驱动程序:
driver = webdriver.Chrome(<absoluteDriverPath>,...)
Run Code Online (Sandbox Code Playgroud)将尖括号内的所有内容替换为其各自的值。
要找出要添加到 crontab 的显示,请使用:
env | grep 'DISPLAY'
Run Code Online (Sandbox Code Playgroud)
然后将这段添加到您的 crontab 命令中:
export DISPLAY=:1
Run Code Online (Sandbox Code Playgroud)
如果您有非默认 shell*,请设置该 shell。使用两个命令之一找出 shell 的位置
which bash
which zsh
Run Code Online (Sandbox Code Playgroud)
然后将 shell 设置为上一个命令的响应(在 crontab 中):
SHELL=/bin/zsh
Run Code Online (Sandbox Code Playgroud)将以下内容之一添加到 crontab 命令中,具体取决于您使用的是 bash 还是 zsh:
source /home/<username>/.zshrc
source /home/<username>/.bashrc
Run Code Online (Sandbox Code Playgroud)
初始化驱动程序时,使用以下指向 selenium chrome 驱动程序的行。
driver = webdriver.Chrome(<absoluteDriverPath>,options=options)
Run Code Online (Sandbox Code Playgroud)
>> log.log 2>&1
Run Code Online (Sandbox Code Playgroud)
意味着所有输出都写入文件(这可以更轻松地调试 crontab)。
| 归档时间: |
|
| 查看次数: |
4642 次 |
| 最近记录: |