我正在尝试匹配一个变量;在本例中为“Days_in_advance”,其中输出为当前日期 + 7 天,例如该月的第 10 天。
我希望将此“Days_in_advance”值与 text() 相匹配,以找到完全匹配的元素,然后单击它。
目前有这样的东西,屏幕截图中突出显示的行是我想要匹配的值。
提前天数 = date.today() + datetime.timedelta(天 = 7) formatted_date = days_in_advance.strftime("%d")
datepick = driver.find_element(By.XPATH, "//*[contains(text(), formatted_date)]") datepick.click()
我知道这是错误的,但我对此很陌生,所以我很感谢您的帮助。谢谢!
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from datetime import date, timedelta
# Initialize the driver
driver = webdriver.Chrome()
# Navigate to the page containing the element
driver.get('URL_of_the_page')
# Calculate the date 7 days in advance
days_in_advance = date.today() + timedelta(days=7)
formatted_date = days_in_advance.strftime("%d")
# Print the formatted date for debugging
print(f"Formatted Date: {formatted_date}")
# Find the element containing the formatted date text and click it
xpath_expression = f"//*[contains(text(), '{formatted_date}')]"
datepick = driver.find_element(By.XPATH, xpath_expression)
datepick.click()
# Close the driver (if needed)
# driver.quit()