Python Selenium预订Padel场地:时段可用性检测问题排查
Padel场地预订Selenium代码问题排查与修复
问题背景
需要使用Python结合Selenium实现以下功能:
- 检测指定时段(目标21:00,测试用18:00)的Padel Buiten场地是否空闲
- 按偏好顺序(Padel Buiten 3、2、4、6、7)优先选择空闲场地进行预订
实际测试中发现:18:00时段4和6场地明明空闲,但现有代码却输出“No available courts found”,需要排查并修复代码逻辑。
附目标时段HTML结构示例(21:00时段):
<tr utc="1732564800" data-time="21:00"> <th class="matrix-timeslot empty"></th> <td class="slot normal r-2992 s-1280 taken" type="taken" finishes="1732568400" id="matrix-res-11317517" tooltip="Padel Buiten 1:::" rowspan="4"><div class="slot-period"> 21:00</div><div class="players"> <b>Gereserveerd</b></div></td> <td class="slot normal r-4435 s-1280 free peak" type="free" slot="4435" title="Padel Buiten 2" rowspan="4"><div class="slot-period"> 21:00</div></td> <td class="slot normal r-4436 s-1280 free peak" type="free" slot="4436" title="Padel Buiten 3" rowspan="4"><div class="slot-period"> 21:00</div></td> <td class="slot normal r-4437 s-1280 taken" type="taken" finishes="1732568400" id="matrix-res-11310116" tooltip="Padel Buiten 4:::" rowspan="4"><div class="slot-period"> 21:00</div><div class="players"> <b>Gereserveerd</b></div></td> <td class="slot normal r-4616 s-1280 blocked b-133207 by-blockcourt" type="blocked" slot="133207" tooltip="Padel lessen (11-11-2024 - 10-02-2025)<br>(17:00 - 22:00)"><div class="reason"> Padel lessen</div></td> <td class="slot normal r-4617 s-1280 taken" type="taken" finishes="1732568400" id="matrix-res-11310115" tooltip="Padel Buiten 6:::" rowspan="4"><div class="slot-period"> 21:00</div><div class="players"> <b>Gereserveerd</b></div></td> <td class="slot normal r-4618 s-1280 taken" type="taken" finishes="1732568400" id="matrix-res-11311773" tooltip="Padel Buiten 7:::" rowspan="4"><div class="slot-period"> 21:00</div><div class="players"> <b>Gereserveerd</b></div></td> <td class="slot normal r-4619 s-1280 blocked b-126740 by-blockcourt" type="blocked" slot="126740" tooltip="Reserveren via Playtomic (21-06-2024 - 31-12-2024)<br>(07:00 - 23:55)"><div class="reason"> Reserveren via Playtomic</div></td> </tr>
现有代码问题分析
- XPath文本匹配不精确:原代码中
./div[text()='18:00']要求div文本完全等于18:00,但实际HTML中div文本带有前导空格(如21:00),导致匹配失败。 - 循环逻辑错误:
if available:语句放在for循环外部,只会检查最后一个场地的结果,前面的场地匹配结果被覆盖,无法按偏好顺序检测。 - 变量赋值错误:
baan_boeking = court_preferences会将整个偏好列表赋值,而不是当前找到的空闲场地编号。 - 依赖强制等待:仅用
time.sleep()无法确保页面完全加载,可能导致元素未渲染完成就执行检测。
修正后的代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import sys # 配置目标时段,测试时改为'18:00',正式用'21:00' TARGET_TIME = '18:00' # 场地偏好顺序 court_preferences = ['3', '2', '4', '6', '7'] # 初始化WebDriver driver = webdriver.Chrome() driver.get("https://bent.baanreserveren.nl") try: # 等待登录元素加载,替代time.sleep() WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "username")) ) # 登录操作 driver.find_element(By.NAME, "username").send_keys("kbolk") driver.find_element(By.NAME, "password").send_keys("pw") driver.find_element(By.CSS_SELECTOR, ".form-submit3 > .button3").click() # 切换到目标日期(点击3次导航按钮) for _ in range(3): date_nav = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, ".matrix-date-nav:nth-child(3)")) ) date_nav.click() time.sleep(1) # 选择Padel Buiten项目 dropdown = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "matrix-sport")) ) dropdown.click() option = dropdown.find_element(By.XPATH, "//option[. = 'Padel Buiten']") option.click() dropdown.click() # 关闭下拉菜单 time.sleep(1) # 按偏好顺序检测空闲场地 booked = False selected_court = None for court in court_preferences: # 修正XPath:处理文本前导空格,使用normalize-space去除前后空白 xpath = f"//td[@type='free'][@title='Padel Buiten {court}']//div[normalize-space(text())='{TARGET_TIME}']" available = driver.find_elements(By.XPATH, xpath) if available: selected_court = court booked = True print(f"找到空闲场地:Padel Buiten {selected_court}") break # 找到偏好最高的场地后立即停止检测 if not booked: print("No available courts found") sys.exit() # 此处添加预订后续步骤 print("执行预订后续步骤") finally: # 可根据需求选择是否关闭浏览器 # driver.quit() pass
关键修改说明
- XPath优化:使用
normalize-space(text())去除文本前后空格,确保时段匹配准确。 - 循环逻辑修正:将
if available:放入循环内部,找到第一个偏好空闲场地后立即跳出循环,符合优先级要求。 - 显式等待替代强制等待:使用
WebDriverWait确保元素加载完成,提升代码稳定性。 - 变量赋值修正:将
selected_court赋值为当前找到的空闲场地编号,方便后续预订流程调用。
内容的提问来源于stack exchange,提问作者Wesley Bolk
相关产品推荐
相关产品推荐

