I have been trying to run a selenium Chrome browser using pyvirtualdisplay inside a flask application as API on startup.
This is the code I'm trying to run
from flask import Flask, request, jsonify
from pymongo import MongoClient
import pymongo.errors
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chromeOptions.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome("/usr/local/bin/chromedriver", chrome_options=chromeOptions)
application = Flask(__name__)
if __name__ == "__main__":
application.run(host='0.0.0.0')
browser.close()
display.stop()
It is working fine when trying to run as
python project.py
But when deployed using uswgi and Nginx and while trying to start it throws the error
File "./wsgi.py", line 1, in from project import application File "./project.py", line 7, in display = Display(visible=0, size=(800, 600)) File "/home/ubuntu/project/projectenv/local/lib/python2.7/site-packages/pyvirtualdisplay/display.py", line 34, in init self._obj = self.display_class( File "/home/ubuntu/project/projectenv/local/lib/python2.7/site-packages/pyvirtualdisplay/display.py", line 52, in display_class cls.check_installed() File "/home/ubuntu/project/projectenv/local/lib/python2.7/site-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed ubuntu_package=PACKAGE).check_installed() File "/home/ubuntu/project/projectenv/local/lib/python2.7/site-packages/easyprocess/init.py", line 180, in check_installed raise EasyProcessCheckInstalledError(self) easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help'] OSError=[Errno 2] No such file or directory Program install error! You can install it in terminal: sudo apt-get install xvfb
unable to load app 0 (mountpoint='') (callable not found or import error)
Thanks