The following isnt quite working:

import time
import os
import random
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from dotenv import load_dotenv

# ----------------------------- Configuration ----------------------------- #

# Load environment variables from .env file
load_dotenv()

# Twitter Credentials
TWITTER_USERNAME = os.getenv('TWITTER_USERNAME')      # 'your_username'
TWITTER_PASSWORD = os.getenv('TWITTER_PASSWORD')      # 'your_new_secure_password'

# Target Twitter Profile
TARGET_USERNAME = os.getenv('TARGET_USERNAME')        # 'thegorillagripp'

# Maximum Scrolls
MAX_SCROLLS = 5                                         # Adjust as needed

# ----------------------------- Setup Selenium ----------------------------- #

def init_driver():
    chrome_options = Options()
    # Ensure headless mode is disabled for visibility
    # chrome_options.add_argument("--headless")           # Should remain commented out
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("--lang=en")
    chrome_options.add_argument("--log-level=3")        # Suppress logs
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-popup-blocking")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--incognito")          # Optional: Use incognito mode

    # Set a custom user-agent to mimic a real browser
    chrome_options.add_argument(
        "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/112.0.5615.49 Safari/537.36"
    )

    # Initialize ChromeDriver using webdriver-manager
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)
    # Maximize window to ensure all elements are visible
    driver.maximize_window()
    return driver

# ----------------------------- Twitter Login ----------------------------- #

def twitter_login(driver, username, password):
    driver.get("https://twitter.com/login")
    print("Navigating to Twitter login page...")

    try:
        wait = WebDriverWait(driver, 20)  # Increased wait time

        # Locate username field and enter username
        username_field = wait.until(EC.presence_of_element_located((By.NAME, 'text')))
        username_field.clear()
        username_field.send_keys(username)
        print("Entered username.")

        # Click the 'Next' button
        next_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Next')]")))
        next_button.click()
        print("Clicked 'Next' button.")

        # Random sleep to mimic human behavior
        random_sleep(2, 4)

        # Wait for password field to be visible
        password_field = wait.until(EC.presence_of_element_located((By.NAME, 'password')))
        password_field.clear()
        password_field.send_keys(password)
        print("Entered password.")

        # Click the 'Log in' button
        login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Log in')]")))
        login_button.click()
        print("Clicked 'Log in' button.")

        # Verify successful login by checking for home icon
        wait.until(EC.presence_of_element_located((By.XPATH, '//a[@href="/home"]')))
        print("Successfully logged in to Twitter.")

    except Exception as e:
        print(f"Error during login: {e.__class__.__name__}: {e}")
        # Capture screenshot for debugging
        driver.save_screenshot("login_error.png")
        print("Screenshot of the error page saved as 'login_error.png'.")
        driver.quit()
        exit()

# ----------------------------- Navigate to Profile ----------------------------- #

def navigate_to_profile(driver, target_user):
    profile_url = f"https://twitter.com/{target_user}"
    print(f"Navigating to @{target_user}'s profile at {profile_url}...")
    try:
        driver.get(profile_url)

        wait = WebDriverWait(driver, 20)  # Increased wait time
        # Wait until tweets are loaded by checking for tweet containers
        wait.until(EC.presence_of_element_located((By.XPATH, '//div[@data-testid="tweet"]')))
        print(f"Successfully navigated to @{target_user}'s profile.")

    except Exception as e:
        print(f"Error navigating to profile: {e.__class__.__name__}: {e}")
        # Capture screenshot for debugging
        screenshot_path = f"{target_user}_profile_error.png"
        driver.save_screenshot(screenshot_path)
        print(f"Screenshot saved to {screenshot_path}")
        driver.quit()
        exit()

# ----------------------------- Scroll and Load Tweets ----------------------------- #

def scroll_and_load(driver, max_scrolls=5):
    last_height = driver.execute_script("return document.body.scrollHeight")
    scrolls = 0

    while scrolls < max_scrolls:
        print(f"Scrolling {scrolls + 1}/{max_scrolls}...")
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        # Random sleep to mimic human behavior
        random_sleep(3, 6)
        try:
            new_height = driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
                print("Reached the end of the page or no new tweets loaded.")
                break
            last_height = new_height
            scrolls += 1
        except Exception as e:
            print(f"Error during scrolling: {e.__class__.__name__}: {e}")
            break
    print("Completed scrolling.")

# ----------------------------- Extract Video Tweets ----------------------------- #

def extract_videos(driver):
    print("Extracting video tweets using Selenium...")
    try:
        tweets = driver.find_elements(By.XPATH, '//div[@data-testid="tweet"]')
        print(f"Found {len(tweets)} tweets on the page.")
        video_data = []

        for tweet in tweets:
            try:
                # Check if the tweet contains a video
                videos = tweet.find_elements(By.TAG_NAME, 'video')
                if videos:
                    # Extract view count
                    try:
                        view_count_span = tweet.find_element(By.XPATH, ".//span[contains(text(), 'Views')]")
                        view_text = view_count_span.text
                        view_count = parse_view_count(view_text)
                    except:
                        view_count = 0  # Default if view count not found

                    # Extract tweet text
                    try:
                        tweet_text_div = tweet.find_element(By.XPATH, ".//div[@data-testid='tweetText']")
                        tweet_text = tweet_text_div.text
                    except:
                        tweet_text = ''

                    # Extract tweet URL
                    try:
                        tweet_url_a = tweet.find_element(By.XPATH, ".//a[contains(@href, '/status/')]")
                        tweet_url = tweet_url_a.get_attribute('href')
                    except:
                        tweet_url = ''

                    video_data.append({
                        'Tweet URL': tweet_url,
                        'Text': tweet_text,
                        'View Count': view_count
                    })
            except Exception as e:
                print(f"Error extracting a tweet: {e.__class__.__name__}: {e}")
                continue  # Skip if any error occurs

        print(f"Extracted {len(video_data)} video tweets.")
        return video_data
    except Exception as e:
        print(f"Error during video extraction: {e.__class__.__name__}: {e}")
        return []

def parse_view_count(view_text):
    """
    Parses view count text and converts it to an integer.
    Examples:
        '1.2K Views' -> 1200
        '34 Views' -> 34
    """
    try:
        parts = view_text.split(' ')[0]
        if 'K' in parts:
            return int(float(parts.replace('K', '')) * 1000)
        elif 'M' in parts:
            return int(float(parts.replace('M', '')) * 1000000)
        else:
            return int(parts.replace(',', ''))
    except:
        return 0

# ----------------------------- Utility Functions ----------------------------- #

def random_sleep(min_seconds=2, max_seconds=5):
    """Sleeps for a random duration between min_seconds and max_seconds."""
    sleep_duration = random.uniform(min_seconds, max_seconds)
    print(f"Sleeping for {sleep_duration:.2f} seconds...")
    time.sleep(sleep_duration)

# ----------------------------- Main Function ----------------------------- #

def main():
    driver = init_driver()

    try:
        # Login to Twitter
        print("Logging into Twitter...")
        twitter_login(driver, TWITTER_USERNAME, TWITTER_PASSWORD)

        # Navigate to target profile
        print(f"Navigating to @{TARGET_USERNAME}'s profile...")
        navigate_to_profile(driver, TARGET_USERNAME)

        # Scroll to load tweets
        print("Scrolling to load tweets...")
        scroll_and_load(driver, MAX_SCROLLS)

        # Extract video tweets
        print("Extracting video tweets...")
        videos = extract_videos(driver)

        if not videos:
            print("No videos found on this profile.")
            return

        # Sort videos by view count
        sorted_videos = sorted(videos, key=lambda x: x['View Count'], reverse=True)

        # Display the sorted videos
        df = pd.DataFrame(sorted_videos)
        print("\nVideos sorted by view count:\n")
        print(df)

        # Optionally, export to CSV
        df.to_csv('twitter_videos_sorted.csv', index=False)
        print("\nData exported to 'twitter_videos_sorted.csv'")

    except Exception as e:
        print(f"An unexpected error occurred: {e.__class__.__name__}: {e}")
    finally:
        driver.quit()

if __name__ == "__main__":
    main()