无法使用Selenium在日历中选择明天的日期
我尝试过使用find_element_by_css_selector和find_element_by_xpath,但是似乎没有什么效果。
import time
import datetime
from time import strftime
from selenium import webdriver
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe")
browser.get('https://www.****.com')
browser.find_element_by_id("UserName").send_keys("***")
browser.find_element_by_id("password").send_keys("***")
browser.find_element_by_id("submit-sign-in").click()
browser.find_element_by_id("Information").click()
browser.find_element_by_id("Amenities").click()
browser.find_element_by_id("reserve").click()
browser.find_element_by_id("resv-date").click()
******#The code works fine until here**********
#Trying to pick tomorrows date from the calendar.
browser.find_element_by_css_selector("a.ui-state-default.ui-state-active").click()
Not getting an error message but it doesn't select the required date
现附上以下图片:
<td class=" ui-datepicker-today" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<td class=" ui-datepicker-week-end ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-active" href="#">18</a></td>
<a class="ui-state-default ui-state-active" href="#">18</a>
片段日历选择器
发布于 2019-05-17 11:13:05
您必须使用下面的css来选择第二天。
td.ui-datepicker-current-day + td
代码:
browser.find_element_by_css_selector("td.ui-datepicker-current-day+td").click()
+
相邻的兄弟组合器。如果在当前示例中考虑到,td.ui-datepicker-current-day
正在选择当前的日期,即17
,但我们希望选择以下兄弟姐妹,因此使用+
将选择同级,并指定筛选相邻的兄弟姐妹为td
。
如果您想在相同的情况下使用xpath,那么您可以使用following-sibling
并通过标记名td
进行筛选,如下所示。
//td[contains(@class, 'ui-datepicker-current-day')]/following-sibling::td
简而言之,css中的+
几乎等同于xpath中的following-sibling
,这将有助于选择同级项。
请参阅上面讨论的方法上的CSS和Xpath的以下链接。
https://stackoverflow.com/questions/56192168
复制相似问题