我需要运行一个使用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
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作为远程网络驱动程序,但结果都是一样的(Chrome在显然无法连接到虚拟显示器后退出)