pythonwebdriver脚本在控制台中正常,但赢得';不要作为一个新贵服务开始

pythonwebdriver脚本在控制台中正常,但赢得';不要作为一个新贵服务开始,python,linux,bash,selenium-webdriver,upstart,Python,Linux,Bash,Selenium Webdriver,Upstart,我需要运行一个使用Selenium webdriver的Python脚本: import platform from selenium import webdriver from pyvirtualdisplay import Display driver = None if platform.system() == 'Linux': print("Initializing browser for chrome...") DISPLAY = Display(visible=0

我需要运行一个使用Selenium webdriver的Python脚本:

import platform
from selenium import webdriver
from pyvirtualdisplay import Display

driver = None

if platform.system() == 'Linux':
    print("Initializing browser for chrome...")
    DISPLAY = Display(visible=0, size=(800, 600))
    DISPLAY.start()
    print("Started display")
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=chrome_options)
    print("Done init chrome")
    connected = True
else:
    try:
        print("Starting firefox...")
        driver = webdriver.Firefox()
        connected = True
    except:
        print("Could not connect through firefox")

if driver:
    print("driver ok")
    driver.quit()
    print("All ok")
脚本从控制台正常运行:

sudo ~/environments/scrapers/bin/python test_webdriver.py
Initializing browser for chrome...
Started display
Done init chrome
driver ok
All ok
但是,如果尝试使用具有Upstart的exec节运行,则会出现WebDriverException错误:

Initializing browser for chrome...
Started display
...
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.8.0-58-generic x86_64)
我在upstart脚本中添加了可能缺少的路径,如下所示

env PYTHON_HOME=/home/rsa-key-20161031/environments/scrapers
env PATH=/usr/local/bin:/usr/bin:$PYTHON_HOME:$PATH
env ENV=production
env DISPLAY=:10
chdir /home/user/project
console log
exec $PYTHON_HOME/bin/python test_webdriver.py
没有效果。“搜索错误”不会给出任何特定于此的内容。 任何关于如何让这项工作的见解都是非常感谢的

更新:
我目前的解决方案是使用Cron,因为它在使用Xvfb时似乎没有问题。我仍然非常想知道是否可以将webdriver任务作为服务运行。我还尝试使用Selenium作为远程webdriver,但结果是一样的(Chrome在显然无法连接到虚拟显示器后退出)

我遇到了类似的情况,而我将pytest配置为使用Selenium和firefox webdriver运行

作为一种解决方案,您可以在debian中安装
xvfb

sudo apt install xvfb
现在,您可以在
exec
位置之后调整upstart文件,以便使用脚本作为参数调用xvfb

xvfb-run --server-num=10 <script>
xvfb运行——服务器数量=10

这样,
xvfb
在脚本前面启动。

Upstart没有启动Chrome的X-Windows会话。Upstart和其他Linux服务管理系统(systemd、sysVinit)不是为使用依赖GUI的服务而设计的;我将Pyvirtualdisplay/start Xvfb作为一个单独的进程使用,这会改变什么吗?好主意,谢谢;在尝试这个的时候,;启动Xvfb run时会出现以下错误:
/usr/bin/Xvfb run:mktemp:notfound
,而系统中存在mktemp。有什么想法吗?@AlexBausk很高兴听到它几乎能起作用。我猜
mktemp
不是PATH变量的一部分;因此,键入
which mktemp
(在我的例子中是
/bin
)并添加带有冒号的路径,即
env path=/bin:/usr/local/bin:…
哇,它现在真的可以工作了。我非常感谢你的帮助!这一直困扰着我