带有chromedriver的硒无法通过cron启动 [英] Selenium with chromedriver doesn't start via cron

查看:75
本文介绍了带有chromedriver的硒无法通过cron启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SentOS7上以 headless模式使用Selenium和Chromedriver的Python脚本可以很好地运行.

Python script with Selenium and Chromedriver in headless mode on CentOS7 runs fine when called manually.

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

但是,使用crontab启动脚本时,它将在第4行(上面)引发此异常.底部有完整的追溯.

When starting script with crontab however it throws this exception at line 4 (above). Full traceback at bottom.

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

使用crontab -e设置Cron

Cron is setup with crontab -e

* * * * * 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的路径

This produced errors like chromedriver couldn't be found. I then added following to crontab -e.
1) Use bash instead of sh, although starting python script manually from sh works fine
2) Specify path to chromedriver

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

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

I tried different suggestions found on the web like adding --no-sandbox options to chromedriver in my script. All didn't help. Please note that I am using chrome in headless mode, so I think I don't need this export DISPLAY=:0 stuff in cron, or Xvfb libs as it used to be.

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

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)

推荐答案

最终找到了解决方案.男孩,这困扰了我太久了.问题是由于缺少PATH目录:cron中的/usr/bin,/usr/sbin.完整的cron现在看起来像这样:

Finally found the solution. Boy this was bugging me for far too long. Issue was following missing PATH directories: /usr/bin, /usr/sbin in cron. Complete cron looks now like this:

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

这篇关于带有chromedriver的硒无法通过cron启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆