含有氯化物的硒不是通过cron开始的

时间:2018-05-01 13:28:50

标签: python selenium cron 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)

使用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/

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

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)

2 个答案:

答案 0 :(得分:1)

对我有用的是以下步骤:

  1. 在我的crontab中添加“ DISPLAY =:1”
  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. 使用以下行初始化硒chrome驱动程序:

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

将尖括号内的所有内容替换为其各自的值。

1。设置显示

要确定要添加到crontab中的显示内容,请使用:

    env | grep 'DISPLAY'

然后将其添加到您的crontab命令中:

    export DISPLAY=:1

2。设置外壳

  • 如果具有非默认的shell *,则设置该shell。 使用两个命令之一找出外壳的位置

    which bash
    which zsh
    

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

    SHELL=/bin/zsh
    

3。获取环境变量

根据您使用bash或zsh的不同,将以下内容之一添加到crontab命令中:

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

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

初始化驱动程序时,请使用以下行指向硒铬驱动程序。

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

其他

    >> log.log 2>&1

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

答案 1 :(得分:1)

在ubuntu 18.04,Python 3.6.9:

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

我的硒调用:

User user = new User();
user.dress = new Dress();  //just do this to avoid that error.
user.UserID = "name";
user.Gender = "f";
user.Height = 180;
user.dress.waist = new int[1, 1];
user.dress.hips = new int[1, 1];
user.dress.chest = new int[1, 1];

我在crontab中放了什么:

#!/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)

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