'Python Selenium works when run individually but does not work when run through Flask

When I run my python selenium code individually, either through Jupyter or Sublime Text, it works (I tested with and without --headless). To keep it simple for this, I tested my code opening google finance's page on AAPL and then prompting selenium to click on 'Tim Cook' which it does (called test()). To make it more versatile, I wanted to execute the code from a flask website. When I run flask and click the command button which should run the selenium (and this time without --headless) the browser window opens on the correct URL but it does not do anything after that; it just stays their, until, I suppose, my driver.quit() is called and the browser windows closes automatically.

For your information, my actual python code is not opening Google Finance but that, too, faces the same problem. The correct URL opens, which was passed in as driver.get(website), but then nothing happens. The window, in a few seconds, closes automatically. On the other hand, when I run this code individually it completes and I can see it happening with or without --headless.

As for my Flask code, I have a templates folder with the templates (they work perfectly) and my code seems correct, too. Here's the code for that google finance program:

from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys  # keys allows sending keystrokes
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as E
from flask import Flask, request, redirect, render_template, url_for
import time

app = Flask(__name__)


@app.route('/')
def home():
    return render_template('welcome.html')


@app.route('/login', methods=['GET', 'POST'])
def login():

    if request.method == 'POST':
        # username = request.form['username']
        # password = request.form['password']
        # This information would be used in my actual function but commented for now
        # main(username, password)
        test()

        # return redirect(url_for("main", usr=username, password=password))
        return render_template('success.html')

    else:
        return render_template('login.html')
    
def test():
    driver_path = "D:/Edge Webdriver/edgedriver_win32/msedgedriver.exe"  # MS Edge Driver location
    options = EdgeOptions()  # EdgeOptions class object
    # options.add_argument("--headless")  # Use Selenium without showing browser window
    options.use_chromium = True  # Chromium Edge
    website = 'https://www.google.com/finance/quote/aapl:NASDAQ'    

    driver = Edge(driver_path, options=options)
    driver.get(website)
    wait_time = 10


    try:
        next_page_2 = WebDriverWait(driver, wait_time).until(
            EC.presence_of_element_located((By.CLASS_NAME, "tBHE4e"))
        )
        next_page_2.click()
    except:
        pass

    driver.quit()

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

Again, the templates work just fine and the Flask website on http://127.0.0.1:5000/ also opens perfectly.

What am I missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source