javascript - 如何从硒中的日期选择器中选择今天的日期和时间(+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 分钟时间从日期选择器。
我很难找到伙计们,任何帮助将不胜感激
解决方案
这个问题缺少一些细节,例如时间选择的 html 页面看起来如何,但无论如何我会尝试猜测并帮助从 qestion 中获得什么。
由于您无法选择当前日期,因此最有可能发生这种情况是因为您的条件未得到满足:
if (cell.getText().equals(today))
单元格是 a div
,它getText()
应该分别返回28
、29
和30
。然后你将它与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
推荐阅读
- c - 使用结构和 char* 已损坏但其他数据字段不是?
- excel - 根据条件复制数据
- docker - 如何在 Docker Hub 中获取 Unix 映像
- install4j - createbundle 命令行拒绝在 Windows 上生成 Mac 包
- javascript - Javascript:使用 fetch 将数据发布到 PHP
- mysql - MySQL Workbench:检查2行数据后插入行,循环整个表
- marklogic - CloudWatch 与 MarkLogic DHS 的集成
- html - 需要帮助打包脚本以放置在头部(Wordpress)
- python - 我想使用 matplotlib 在饼图聊天中绘制泰坦尼克数据集的性别(男性/女性)比例,显示男性和女性的百分比表示
- flutter - ListView 在显示键盘后调整大小