【问题标题】:How to select the next day from the calendar using Selenium-Python如何使用 Selenium-Python 从日历中选择第二天
【发布时间】:2019-05-17 19:01:18
【问题描述】:

无法使用 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>

片段 Calendar Date picker

【问题讨论】:

    标签: selenium datepicker calendar


    【解决方案1】:

    你必须使用下面的 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 的链接。

    CSS

    【讨论】:

    • 更新 css 以选择当天的下一个 td。
    • 如果您可以选择第二天,请告诉我。
    • 还可以解释一下“td.ui-datepicker-current-day+td”是如何工作的?我只是好奇,不知道 +td 的用法。
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2021-01-20
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2017-09-11
    相关资源
    最近更新 更多