迭代日期选择器中的日期,找到可用日期,然后选择其中之一。

huangapple go评论81阅读模式
英文:

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, &quot;//div[@class=&#39;datepicker-days&#39;]/table/tbody/tr/td[not(contains(@class, &#39;disabled&#39;))]&quot;) 

for date in available_dates:
 print(date.text)

Available date

Line of code 23th September

&lt;td class=&quot;day activeClass&quot;title=&quot;Book&quot;&gt;23&lt;/td&gt;

答案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(&#39;https://example.com&#39;)
    try:
    	print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, &quot;div.datepicker-days &gt; table.table-condensed tr td.day.activeClass[title=&#39;Book&#39;]&quot;)))])
    except TimeoutException:
    	print(&quot;No booking dates available for the current month&quot;)
    driver.quit()
    
  • Using XPATH and get_attribute(&quot;innerHTML&quot;):

    driver.get(&#39;https://example.com&#39;)
    try:
    	print([my_elem.get_attribute(&quot;innerHTML&quot;) for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, &quot;//div[@class=&#39;datepicker-days&#39;]/table[@class=&#39; table-condensed&#39;]//tr//td[@class=&#39;day activeClass&#39; and @title=&#39;Book&#39;]&quot;)))])
    except TimeoutException:
    	print(&quot;No booking dates available for the current month&quot;)
    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
    

huangapple
  • 本文由 发表于 2023年2月8日 19:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定