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

如何使用Selenium和Python点击无Button类的指定时段按钮?

无Button类时用Selenium Python点击时段按钮的方法
  • 根据时段文本直接定位
    如果按钮文本包含明确时段(如“下午4:34”),可以用XPath精准匹配:
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

driver = webdriver.Chrome()
# 等待按钮加载完成后点击
target_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//*[text()='下午4:34']"))
)
target_button.click()

若文本存在空格或需部分匹配,改用contains:

target_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), '下午4:34')]"))
)
  • 通过关联的Jump编号定位
    如果时段按钮和“Jump 65”同属一个父容器,可先定位Jump文本再关联按钮:
# 假设Jump 65与时段按钮在同一父元素下
target_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//*[text()='Jump 65']/../*[contains(text(), '下午4:34')]"))
)
target_button.click()
  • 利用其他独有属性定位
    如果按钮有唯一的data-*、id或专属class(非通用Button类),直接通过属性定位:
# 示例:通过data-time属性定位
target_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "[data-time='16:34']"))
)
# 示例:通过专属class定位
target_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, ".time-slot-item"))
)
  • 遍历所有时段按钮筛选点击
    若以上方法不适用,可遍历所有时段按钮,匹配目标文本后点击:
time_slots = WebDriverWait(driver, 10).until(
    EC.presence_of_all_elements_located((By.XPATH, "//*[contains(text(), '上午') or contains(text(), '下午')]"))
)
for slot in time_slots:
    if "下午4:34" in slot.text:
        slot.click()
        break

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:55:31