带有chromedriver的硒无法通过cron启动

编程入门 行业动态 更新时间:2024-10-27 08:25:57
本文介绍了带有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.

seleniummon.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) seleniummon.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启动

本文发布于:2023-11-27 20:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:chromedriver   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!