Selenium使用Python.如何找到合适的元素



我需要在网站上选择日期。我需要该程序点击当前日期。幸运的是,他有一个不同级别的class="ui-state-default ui-state-highlight"

日期的选择分为5个相同的块,其中还有7个相同的带有日期的块。

我需要在所有的日子里找到当前的一天,当我开始浏览所有的日子,看看那里是否有我需要的一行时,我会收到一条错误消息。

我的完整代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('**site**')
element = driver.find_element(By.XPATH, "//div[@class='ait-schedlfind-datepickers']")
option = element.find_element(By.XPATH, "//input[@class='ait-schedlfind-cal1 hasDatepicker']")
option.click()
element = driver.find_element(By.XPATH, "//div[@class='ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ait-schedl-picker']")
table = element.find_element(By.XPATH, "//table[@class='ui-datepicker-calendar']")
tr = table.find_elements(By.TAG_NAME, "tr")
for v in tr:
td = v.find_elements(By.TAG_NAME, "td")
for k in td:
currect_day = k.find_elements(By.XPATH, "//a[@class='ui-state-default ui-state-highlight']")

我认为您不需要循环来选择所需的元素。option.click()之后的所有内容都可以替换为:

correct_day = driver.find_element(By.XPATH, "//*[contains(@class, 'ui-state-highlight')]")
# You can check that it's the correct day with
# print(correct_day.text)

最新更新