0

I'm trying to use Pyvirtualdisplay in my Flask backend, but it can't find Xvfb. This is my code:

@app.route('/test-selenium', methods=('GET',))
def test_selenium():
    display = Display(visible=0, size=(800, 600))
    display.start()

    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')

    driver = webdriver.Chrome(options=options)
    driver.get('https://www.amazon.com/')
    title = driver.title

    display.stop()

    return title

If I create a venv, pip3 install selenium pyvirtualdisplay, and sudo apt install xfvb, and then python3 test_selenium.py and it returns Amazon.com as expected.

However, when I put that code in my Flask app like above, and try to run access the webpage which should show "Amazon.com", it gives this error:

FileNotFoundError: [Errno 2] No such file or directory: 'Xvfb'

And I just can't figure it out.

I tried the two other pyvirtualdisplay backend options, xephyr and xvnc, and they have the same error.

Is it a permissions issue? My Flask app runs under the same user as the python script that works, but it's in the www-data group. Or do I need to add Xvfb to the Path? I'm not sure why my python script can find it just fine while my Flask app can't.

Thank you for your help!

Update: It might be a path issue. In gunicorn service I wrote:

[Service]
User=Joe
Group=www-data
WorkingDirectory=/home/Joe/flask-app
Environment="PATH=/home/Joe/flask-app/venv/bin"

It looks like it sets the PATH rather than adding to it. I'll try changing that and see if it works.

Update: if I set it to run as root it works. So it's definitely a permissions issue.

0 Answers0