首页 > 解决方案 > 如何从硒中的日期选择器中选择今天的日期和时间(+15 分钟)

问题描述

我无法从 selenium 中的日期选择器中选择今天的日期和时间(加上 15 分钟的时间)。

现在我已经编写了日期代码,但它不起作用所以这里是代码 -

 DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    System.out.println("Format defined");
    Date date2 = new Date();
    System.out.println("Date object creation");
    String today = dateFormat2.format(date2);
    WebElement dateWidget = driver.findElement(By.xpath(".//*[@class='xdsoft_calendar']"));
        System.out.println("Calendar web element");
        List<WebElement> columns=dateWidget.findElements(By.tagName("div"));
        System.out.println("listing web element");
        System.out.println(today);
        //comparing the text of cell with today's date and clicking it.
        for (WebElement cell : columns)
        {
            System.out.println("In for loop"+cell.getText());
            if (cell.getText().equals(today))
            {
                System.out.println("inside if");
                System.out.println(today);
                cell.click();
                break;
            }
        }

日期选择器 日期选择器

<div class="xdsoft_calendar">
   <table>
      <thead>
         <tr>
            <th>Sun</th>
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thu</th>
            <th>Fri</th>
            <th>Sat</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td data-date="28" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week0 xdsoft_date xdsoft_disabled xdsoft_other_month xdsoft_weekend">
               <div>28</div>
            </td>
            <td data-date="29" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week1 xdsoft_date xdsoft_disabled xdsoft_other_month">
               <div>29</div>
            </td>
            <td data-date="30" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week2 xdsoft_date xdsoft_disabled xdsoft_other_month">
               <div>30</div>
            </td>
         </tr>
      </tbody>
   </table>
</div>

注意:1.预期我们应该能够选择今天的日期 2.当前时间 + 15 分钟时间从日期选择器。

我很难找到伙计们,任何帮助将不胜感激

标签: javascriptjavaseleniumselenium-webdriver

解决方案


这个问题缺少一些细节,例如时间选择的 html 页面看起来如何,但无论如何我会尝试猜测并帮助从 qestion 中获得什么。

由于您无法选择当前日期,因此最有可能发生这种情况是因为您的条件未得到满足:

if (cell.getText().equals(today))

单元格是 a div,它getText()应该分别返回282930。然后你将它与dateFormat2.format(date2);最有可能返回类似08/11/2018 02:12 PM. 尝试以下修改(Java 8+):

int currentDay = LocalDate.now().getDayOfMonth();
if (cell.getText().equals(currentDay)) {

}

对于小时和分钟逻辑,要么添加 html 的外观,要么仅用于LocalDate.now().getHour()获取当前小时并实现类似条件以选择必要的时间。提示:您可能需要滚动到必要的元素。

在此处仔细查看LocalTime


推荐阅读