首页 > 解决方案 > 与标签名称关联的 Selenium Chrome Python 复选框

问题描述

我有一个网页,其中包含与日期(标签)相关的复选框列表。

我希望能够通过网页解析:

  1. 在永远是明天的网页中找到我想要的日期(今天+1)
  2. 查找与找到的日期关联的复选框
  3. 选中该复选框(例如:Baignade du 29 juin 2021)

目前,我使用 ID 找到我想要的 CheckBox 并单击它。

非常感谢您提前提供的帮助。干杯。

链接:https ://ludik.maville.net/Brossard-LudikIC2Prod_Enligne/Pages/Anonyme/Recherche/Page.fr.aspx?m=1

# piscine Reservation Alpha v1
# https://sites.google.com/a/chrominium.org/chromedriver/downloads
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

import time
import datetime


PATH = "/Users/josephhuynhngoctri/Desktop/Python/chromedriver"
driver = webdriver.Chrome(PATH)
action = ActionChains(driver)  # init. action for double-click

# get Web page Brossard for aquatic activities
driver.get("https://ludik.maville.net/Brossard-LudikIC2Prod_Enligne/Pages/Anonyme/Recherche/Page.fr.aspx?m=1")


# click on "Aquatique" button at the top left menu
link = driver.find_element_by_id("ctlHautPage_ctlMenu_ctlLienActivites")
link.click()

time.sleep(2)

# CHOOSE DATE
# select checkbox 29 Juin by ID - choice #10
checkbox = driver.find_element_by_id("ctlBlocRecherche_ctlRestrictions_ctlSelSession_ctlListeSessions_ctl10_ctlSession_ctlSelection")
checkbox.click()

# Find TOMORROW Date
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1) 
tomorrow = tomorrow.strftime("%B %d, %Y")
print("Today's date String =", tomorrow)

# select checkbox 29 June by xpath via label 
checkbox = driver.find_element_by_xpath("//label/input[contains(..'tomorrow')]")
checkbox = click()

图片

标签: pythondateselenium-webdrivercheckboxlabel

解决方案


推荐阅读