In my code below, it freezes after printing "Generation Sucessful...". Can you fix it?:

# Importing libraries
import time
import os
from selenium import webdriver
from bs4 import BeautifulSoup
from undetected_chromedriver import options
import undetected_chromedriver as uc
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

# Highest prompt definition
def get_highest_saved_prompt(folder_path):
    saved_prompt_file = os.path.join(folder_path, "saved_prompts.txt")
    if os.path.exists(saved_prompt_file):
        with open(saved_prompt_file, "r") as file:
            content = file.read().strip()
            if content:
                highest_saved_prompt = int(content)
            else:
                highest_saved_prompt = 0
    else:
        highest_saved_prompt = 0
    return highest_saved_prompt

# Update highest prompt definition
def update_highest_saved_prompt(folder_path, highest_saved_prompt):
    saved_prompt_file = os.path.join(folder_path, "saved_prompts.txt")
    with open(saved_prompt_file, "w") as file:
        file.write(str(highest_saved_prompt))

def save_text(driver, element_id, saved_texts, folder_path):
    file_path = r"D:\Projekte\Image Gen v2\Prompts\highest prompt\highest_prompt.txt"  # Initialize the file_path variable
    try:
        element = driver.find_element(By.XPATH, element_id)
        text = element.text
        if text not in saved_texts:
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            file_number = len(saved_texts) * 2
            file_name = f"{file_number}.txt"
            file_path = os.path.join(folder_path, file_name)
            with open(file_path, "w") as file:
                file.write(text)
            saved_texts.append(text)  # Update the saved_texts list
            print(f"Text saved as: {file_path}")
            return True  # Successful save
        else:
            print(f"Text already saved: {file_path}")
            return False  # Unsuccessful save (text already saved)
    except Exception as e:
        print(f"Failed to save text from: {element_id}. Error: {str(e)}")
        if file_path:
            print(f"Associated file_path: {file_path}")
        return False  # Unsuccessful save (error occurred)

def read_saved_prompts(folder_path):
    saved_texts = []
    for file_name in os.listdir(folder_path):
        if file_name.endswith(".txt"):
            with open(os.path.join(folder_path, file_name), "r") as file:
                text = file.read().strip()
                saved_texts.append(text)
    return saved_texts

# Update are_more_prompts_available function to handle exceptions
def are_more_prompts_available(driver, current_element_number):
    try:
        element_id = f'//*[@id="__next"]/div[1]/div[2]/div/main/div[1]/div/div/div/div[{current_element_number}]/div'
        element = driver.find_element(By.XPATH, element_id)
        return True
    except Exception:
        return False

# Update save_all_prompts function
def save_all_prompts(driver, start_element_number, saved_texts, folder_path):
    current_element_number = start_element_number
    saved_file_paths = []  # Initialize the saved_file_paths list
    while are_more_prompts_available(driver, current_element_number):
        element_id = f'//*[@id="__next"]/div[1]/div[2]/div/main/div[1]/div/div/div/div[{current_element_number}]/div'
        file_path = save_text(driver, element_id, saved_texts, folder_path)
        if file_path:
            saved_texts.append(file_path)  # Update the saved_texts list with the file_path
            saved_file_paths.append(file_path)  # Append the file_path to saved_file_paths
        time.sleep(1)  # Wait for a second to avoid overwhelming the server
        current_element_number += 2  # Increment by 2 to move to the next element
    return saved_file_paths  # Return the saved_file_paths list

# Run program definition
def run_program():
    # Create ChromeOptions object
    options = uc.ChromeOptions()

    # Add proxy argument
    options.add_argument('proxy-server=85.214.250.48:3128')
    options.add_argument('--disable-extensions')
    options.add_argument("--disable-infobars")

    # Add headless argument
    # options.add_argument('--headless')

    # Disable password manager
    prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False}
    options.add_experimental_option("prefs", prefs)

    # Create a new instance of the Chrome driver with the specified options
    driver = uc.Chrome(options=options)
    driver.maximize_window()
    print("Starting Driver...")

    # Open the provided link
    driver.get("https://chat.openai.com/c/4402b1d2-71af-43c0-8e45-803faa01b81b")
    print("Opening Website...")

    # Find and click the "Log in" button
    login_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div[1]/div[2]/div[1]/div/div/button[1]'))
    )
    time.sleep(1)
    login_button.click()
    print("Logging in...")

    # Find and enter the email address
    email_input = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="email-input"]'))
    )
    time.sleep(1)
    email_input.click()
    email_input.send_keys("apexkonto007@gmail.com")

    # Find and click the "Continue" button
    continue_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/main/section/div[2]/button'))
    )
    time.sleep(1)
    continue_button.click()

    # Find and enter the password
    password_input = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="password"]'))
    )
    time.sleep(1)
    password_input.click()
    password_input.send_keys("Ghost_0995")

    # Find and click the "Continue" button
    continue_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/main/section/div/div/div/form/div[2]/button'))
    )
    time.sleep(1)
    continue_button.click()
    print("Login successful...")
    time.sleep(10)

    # Find and click the "Confirm" button
    try:
        confirm_button = WebDriverWait(driver, 2).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="radix-:r1p:"]/div[2]/div/div[2]/button'))
        )
        time.sleep(1)
        confirm_button.click()
        print("Term acceptance sucessful...")
    except TimeoutException:
        print("Term acceptance bypassed...")

    # Set the condition to stop the loop
    stop_condition = False
    error = False

    # Get the folder path
    folder_path = "D:\Projekte\Image Generator\Prompts"

    # Get the highest saved prompt number
    highest_saved_prompt = get_highest_saved_prompt(folder_path)

    # Read already saved prompts
    saved_texts = read_saved_prompts(folder_path)

    # Save all possible findable prompts above the current highest saved prompt
    saved_file_paths = save_all_prompts(driver, highest_saved_prompt + 2, saved_texts, folder_path)
    saved_texts.extend(saved_file_paths)  # Update the saved_texts list

    # Loop through the elements dynamically
    i = highest_saved_prompt + 2
    consecutive_save_failures = 0  # Track consecutive save failures
    while not stop_condition:
        try:
            element_id = f'//*[@id="__next"]/div/div[2]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div[{i}]/div/div'
            if save_text(driver, element_id, saved_texts, folder_path):
                consecutive_save_failures = 0  # Reset consecutive save failures if saving was successful
                time.sleep(1)  # Wait for a second to avoid overwhelming the server
                i += 2
            else:
                consecutive_save_failures += 1  # Increment consecutive save failures

            # Proceed to generate new prompts after an error occurs
            error = False

            # Read text from a file
            with open(r"D:\Projekte\Image Generator\Prompts\Prompt formula\Prompt formula.txt", 'r') as file:
                text = file.read()

            # Find and enter the textarea
            textarea = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="prompt-textarea"]'))
            )
            time.sleep(1)
            textarea.click()

            # Use Javascript to paste the prompt formula and press enter
            driver.execute_script(f"arguments[0].value = `{text}`", textarea)
            time.sleep(1)
            textarea.send_keys(Keys.BACK_SPACE)
            continue_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[2]/div[1]/div[2]/main/div[2]/div[2]/form/div/div/div/button'))
            )
            time.sleep(1)
            continue_button.click()
            print("Generating prompts...")
            time.sleep(10)

            # Wait till completion
            try:
                WebDriverWait(driver, 50).until(
                    EC.invisibility_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[2]/div[1]/div[2]/main/div[2]/div[2]/form/div/div/div/div'))
                    )
                print("Generation successful...")
                time.sleep(1)
            except TimeoutException:
                print("Generation timeout...")
                time.sleep(1)

            # Update the highest saved prompt number
            highest_saved_prompt = len(saved_texts) * 2
            update_highest_saved_prompt(folder_path, highest_saved_prompt)

            # Check the condition to stop the loop
            if error or consecutive_save_failures >= 3 or not are_more_prompts_available(driver, i):
                stop_condition = True

        except Exception as e:
            print("An error occurred:", str(e))
            print("Proceeding to generate new prompts...")
            error = True  # Set the error flag to True
            consecutive_save_failures += 1  # Increment consecutive save failures
            time.sleep(1)
            i += 2

    # Keep the browser running indefinitely
    while driver.window_handles:
        time.sleep(1)

run_program()