0

I am trying to find a way for the code to work (aka the console displays all meal types for each dining hall) without having to quit the driver in the second for loop.

import time
import csv
from datetime import datetime
from pytz import timezone

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC 

from dining_info import *

# Setup timezones for PST to ensure continuity
pst_timezone = timezone('America/Los_Angeles')
pst_now = datetime.now(pst_timezone)

# SETUP VARIABLES FOR TEMPORARY TESTING
todays_date = pst_now.strftime('%m/%d/%Y').lstrip("0").replace("/0", "/")
meal_type = "Breakfast"
dining_hall = "Arrillaga"

# Set up Chrome options
chrome_options = webdriver.ChromeOptions()
# Uncomment the next line to run in headless mode
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')

# Set up WebDriver using WebDriver Manager
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

url = 'https://rdeapps.stanford.edu/dininghallmenu/'
driver.get(url)

# Wait for the date dropdown to be present
date_dropdown = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'MainContent_lstDay'))
)

# Retrieve currently selected date
selected_date = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, '#MainContent_lstDay option[selected="selected"]'))
).get_attribute('value')

# Convert selected date format to match today's date format
selected_date_converted = selected_date.split(' - ')[0]

print("Dates match:", selected_date_converted == todays_date)

    # ------------------------------------------------------------------------------------------------- #

for hall in dining_hall_list:
    # Wait for the dining hall dropdown to be present
    dining_hall_dropdown = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'MainContent_lstLocations'))
    )

    # Select dining_hall from the dining hall dropdown
    Select(dining_hall_dropdown).select_by_value(hall)

    # Retrieve currently selected dining hall
            selected_dining_hall = Select(dining_hall_dropdown).first_selected_option.get_attribute('value')

    print("Selected dining hall:", dining_hall_alias[selected_dining_hall])

    for meal in meal_type_list:
    # Wait for the meal type dropdown to be present
    meal_type_dropdown = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'MainContent_lstMealType'))
    )

        # Select meal_type from the meal type dropdown
        Select(meal_type_dropdown).select_by_value(meal)

        # Retrieve currently selected meal type // using this method because stale element when trying method in line 61
        selected_meal_type = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#MainContent_lstMealType option[selected="selected"]'))
    ).get_attribute('value')

        print("Selected meal type:", selected_meal_type)

    # Close instance of WebDriver and reinstate to stop stale element
    driver.quit()
    time.sleep(0.5)
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.get(url)

# ------------------------------------------------------------------------------------------------- #

# Wait for user input before closing the browser
input("Press enter to close the browser...")
driver.quit()

I’ve tried to refresh the browser and selenium still cant find the element we're trying to access - the only workaround I found was to close and reinstate the webdriver, but I think it's inefficient, so is there a way to not close the browser and still be able to go through the loop fully without getting the stale element not found error?

7
  • I think you can just call driver.get(url) to load the right page again, or driver.back() to go back to the previous page. Commented Jul 10 at 2:59
  • @John Gordon I’ve tried to refresh the browser and selenium still cant find the element we're trying to access -the only workaround I found was to close and reinstate the webdriver, but I think it's inefficient, so is there a way to not close the browser and still be able to go through the loop fully without getting the stale element not found error Commented Jul 10 at 3:19
  • What does "refresh the browser" mean? Commented Jul 10 at 3:40
  • first correct code in question because it has wrong indentation.
    – furas
    Commented Jul 10 at 3:46
  • second: always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. It will be also more readable in question, and more people can see it - so more people may help.
    – furas
    Commented Jul 10 at 3:47

0

Browse other questions tagged or ask your own question.