[Python] Default Text re-appears after being overwritten

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to extract bus prices between 2 cities in Ontario, Canada. I am using Selenium/Python to do this:

    The website is here and it has default cities and dates.

    Here is my Python code:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.keys import Keys
    import time

    url_flixbus = 'https://www.flixbus.ca/'

    # new input values
    input_origin = 'Ottawa (VIA Rail)'
    input_destination = 'Sudbury, ON'
    departure_date = 'Sat, Sep 20'

    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get(url_flixbus)

    # click "Accept Cookies" button
    cookie_btn = driver.find_element(By.XPATH, '//*[@id="uc-center container"]/div[2]/div/div/div/button[2]')
    cookie_btn.click()

    # overwrite values
    origin = driver.find_element(By.ID, 'searchInput-from')
    origin.clear()
    time.sleep(1)
    origin.send_keys(input_origin)
    time.sleep(1)
    #origin.send_keys(Keys.ENTER)

    destination = driver.find_element(By.ID, 'searchInput-to')
    destination.clear()
    time.sleep(1)
    destination.send_keys(input_destination)
    time.sleep(1)

    date = driver.find_element(By.ID, 'dateInput-from')
    date.click()
    time.sleep(1)
    date.send_keys(departure_date)
    #date.send_keys(Keys.ENTER)

    # click 'Search' button
    search_btn = driver.find_element(By.XPATH, '//*[@id="search-mask- component"]/div/div/div/div/div[5]/div/button')
    #search_btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.journey')))
    search_btn.click()`


    The default values for 'origin' and 'destination are 'Toronto' and 'Ottawa', respectively. I want to change them to 'Ottawa' and 'Sudbury'.

    My issue is when I run the 'origin' portion of the code, I see that the value changes from the default value 'Toronto' to the new value 'Ottawa', which is good.

    However, when I run the 'destination' portion, the default changes from 'Ottawa' to 'Sudbury', but then I see that the origin textbox reverts back to 'Toronto'.

    The same thing happens to the 'destination' box when I try to overwrite the date.

    I put in the clear() lines to remedy this problem, but it still persists. I also experimented with telling the program to press "ENTER" like on a keyboard and it still doesn't work as intended (commented out).

    What am I missing?

    Continue reading...

Compartilhe esta Página