Selenium and Flask : how to keep Flask running with background selenium tasks?

Question:

Hello, I have a REPL which uses a Selenium Chrome Drive to get webpages.
They are launched by a BackgroundScheduler and the app has a Flask app to show the logs.

The problem is that it works fine at the beginning but after the Selenium request and after driver.quit(), the Flask app doesn’t respond anymore.

Is there a way to make it work ? Is there an issue with ports or conflicts between Selenium web browsing and Flask server on the same repl ?

I don’t have a public repl to show example right now but i’m trying to create one.
Thanks

Did you tried to change from Flask to another server? Like Gunicorn or Waitress?
I think Flask is not ideal for what you are trying to do.

2 Likes

Yes Gunicorn will work perfectly

@GreetMe Thanks for your messages about Gunicorn
Do you have an example of a simple Repl using Gunicorn and Selenium at the same time ?
Thanks

You can use Gunicorn to serve your Selenium script as a web application. Create a new Python file, for example, app.py, and add the following code:

from flask import Flask
import subprocess

app = Flask(__name__)

@app.route('/')
def run_selenium_script():
result = subprocess.run(['python', 'selenium_script.py'], stdout=subprocess.PIPE)
return result.stdout

if __name__ == '__main__':
app.run()

Open your terminal and navigate to the directory where app.py is located. Run the following command to start Gunicorn:gunicorn app:app
This will start a web server that listens for incoming requests and runs your Selenium script when the root URL is accessed.

Thanks, I’ll try it
I need to change my current process to do that because I did not use a selenium script but the selenium object

You’re welcome, feel free to reach me if I can help more in the future