You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Python修改Flixbus站点后默认文本自动恢复问题求助

问题:Flixbus站点Selenium操作时输入框值自动恢复默认的解决办法

我用Python+Selenium爬取加拿大Flixbus站点的巴士票价,目标是将默认的「多伦多→渥太华」改为出发地:Ottawa (VIA Rail)、目的地:Sudbury, ON,出发日期设为Sat, Sep 20。但执行代码时遇到问题:

  • 修改出发地后显示正常,但修改目的地时,出发地自动恢复为默认的多伦多
  • 修改日期时,目的地又变回默认值

试过用clear()清除输入框,也试过模拟回车键,问题依然存在。附上原代码:

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()

解决思路与修正代码

核心原因

Flixbus的出发/目的地输入框是联想搜索组件,直接输入文本后,必须选择下拉列表里的对应选项,网站的前端JS才会确认输入值。如果只输入文本不选择选项,后续点击其他输入框时,会触发页面的校验逻辑,将未确认的输入重置为默认值。另外直接输入日期也不可靠,需要通过点击日历组件的日期来完成选择。

修正后的代码

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 selenium.webdriver.common.keys import Keys

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

input_origin = 'Ottawa (VIA Rail)'
input_destination = 'Sudbury, ON'
departure_date_text = 'Sat, Sep 20'

# 初始化浏览器与显式等待
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)  # 最长等待10秒,适配页面加载速度
driver.get(url_flixbus)

# 接受Cookie(用文本匹配更稳定)
cookie_btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Accept")]')))
cookie_btn.click()

# 设置出发地:输入后选择下拉建议项
origin_input = wait.until(EC.visibility_of_element_located((By.ID, 'searchInput-from')))
origin_input.clear()
origin_input.send_keys(input_origin)
origin_suggestion = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.autocomplete-results li:first-child')))
origin_suggestion.click()

# 设置目的地:输入后选择下拉建议项
destination_input = wait.until(EC.visibility_of_element_located((By.ID, 'searchInput-to')))
destination_input.clear()
destination_input.send_keys(input_destination)
destination_suggestion = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.autocomplete-results li:first-child')))
destination_suggestion.click()

# 设置出发日期:点击日历对应日期
date_input = wait.until(EC.element_to_be_clickable((By.ID, 'dateInput-from')))
date_input.click()
# 提取日期数字(如20),定位日历中的对应元素
date_num = departure_date_text.split(", ")[1].split(" ")[1]
date_element = wait.until(EC.element_to_be_clickable((By.XPATH, f'//div[contains(@class, "calendar-day") and text()="{date_num}"]')))
date_element.click()

# 点击搜索按钮
search_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]')))
search_btn.click()

关键改进点

  1. 用显式等待替代固定延时:避免因页面加载慢导致的元素未就绪问题,操作更稳定
  2. 选择联想下拉建议:确保网站前端JS确认输入值,防止后续操作触发重置
  3. 点击日历日期元素:替代直接输入文本,保证日期选择被页面正确识别

内容的提问来源于stack exchange,提问作者brooklin7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 10:35:44