英文:
Iterate dates in a datepicker to find available ones then select one of them
问题
我正在尝试编写一个脚本来预约这个日期选择器内的约会。
当日期可用时,它们会显示为绿色。问题是,我不知道它们何时可用,无法直接通过属性(如类或标题)来定位它们。
选择一个可用日期会触发一个用于约会时间的下拉列表,以及一个用于签证类型的下拉列表(隐藏元素)。
我应该如何:
1/ 遍历所有日期,循环直到找到可用的日期,然后选择其中之一?
2/ 遍历约会时间,循环直到找到可用的时间,然后选择其中之一?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import sys
# 找到所有可用日期
available_dates = driver.find_elements(By.XPATH, "//div[@class='datepicker-days']/table/tbody/tr/td[not(contains(@class, 'disabled'))]")
for date in available_dates:
print(date.text)
代码中的日期行 23rd September:
<td class="day activeClass" title="Book">23</td>
希望这有助于你的问题。如果需要更多帮助,请告诉我。
英文:
I am trying to code a script to book appointments inside this datepicker.
When available, the dates are in green. The problem is I don't know when they can be available to locate them directly with an attribute ( class or title...)
Selecting an available date triggers a drop-down list for appointment time and another one for visa type( hidden elements)
How can I :
1/Iterate all dates, loop until available ones are found, select one of them?
2/Iterate appointment times ,loop until available ones are found and select one of them?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import sys
available_dates = driver.find_elements(By.XPATH, "//div[@class='datepicker-days']/table/tbody/tr/td[not(contains(@class, 'disabled'))]")
for date in available_dates:
print(date.text)
Line of code 23th September
<td class="day activeClass"title="Book">23</td>
答案1
得分: 0
为提取可用的预订日期,您必须使用WebDriverWait来诱发visibility_of_all_elements_located(),并使用List Comprehension,您可以使用以下任何一种定位策略:
- 使用_CSS_SELECTOR_和_text_属性:
driver.get('https://example.com')
try:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.datepicker-days > table.table-condensed tr td.day.activeClass[title='Book']")))])
except TimeoutException:
print("本月没有可用的预订日期")
driver.quit()
- 使用_XPATH_和_
get_attribute("innerHTML")
_:
driver.get('https://example.com')
try:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='datepicker-days']/table[@class=' table-condensed']//tr//td[@class='day activeClass' and @title='Book']")))])
except TimeoutException:
print("本月没有可用的预订日期")
driver.quit()
- 注意:您需要添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
英文:
To extract the available booking days you have to induce WebDriverWait for visibility_of_all_elements_located() and using List Comprehension you can use either of the following locator strategies:
-
Using CSS_SELECTOR and text attribute:
driver.get('https://example.com') try: print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.datepicker-days > table.table-condensed tr td.day.activeClass[title='Book']")))]) except TimeoutException: print("No booking dates available for the current month") driver.quit()
-
Using XPATH and
get_attribute("innerHTML")
:driver.get('https://example.com') try: print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='datepicker-days']/table[@class=' table-condensed']//tr//td[@class='day activeClass' and @title='Book']")))]) except TimeoutException: print("No booking dates available for the current month") driver.quit()
-
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论