当前位置: 首页 > 面试题库 >

如何使用Java从selenium中的日历中自动选择特定日期

潘畅
2023-03-14
问题内容

我有一种情况,我必须从日历中选择3天回溯日期。如何使用selenium来自动执行此情况。我正在将Java与selenium一起使用进行自动化。


问题答案:

1)假设您可以在输入字段中输入日期,而日历仅是图标。你可以有这样的帮助方法

    public String threeDaysBefore(){
    String threeDaysBefore = "";
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.add(Calendar.DAY_OF_YEAR, -3);
    Date before = cal.getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    threeDaysBefore = formatter.format(before);
    return threeDaysBefore;
}

然后在代码中

  WebElement calendarManualInput = driver.findElement...// find the manual input field
  calendarManualInput.sendKeys(threeDaysBefore());

2)如果您只能单击日历,那将更加棘手。您仍然需要String,但有一点不同:

    public String threeDaysBefore(){
    String threeDaysBefore = "";
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.add(Calendar.DAY_OF_YEAR, -3);
    Date before = cal.getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("dd");
    threeDaysBefore = formatter.format(before);
    return threeDaysBefore;
}

但是,上面没有什么收获。如果日期是1.4。那么它将返回“ 29”,可以解释为29.4。你不想发生。因此,在代码后面,您可能必须这样做

//this will click three days before
Date today = new Date();
Date minusThree = new Date();
Calendar now = Calendar.getInstance();
now.setTime(today);
Calendar before = Calendar.getInstance();
before.setTime(minusThree);
before.add(Calendar.DAY_OF_YEAR, -3);
int monthNow = now.get(Calendar.MONTH);
int monthBefore = before.get(Calendar.MONTH);

if (monthBefore < monthNow){
  // click previous month in the calendar tooltip on page
}
WebElement dateToSelect = driver.findElement(By.xpath("//span[text()='"+threeDaysBefore()+"']"));
dateToSelect.click();


 类似资料:
  • HTML与它的外观截图一起粘贴,如下所示。 HTML: 我曾经试过 甚至试图等待命令以使定位器被找到但没有成功。 WebDriverWait(驱动程序,100)。直到(预期条件.element可禁用)(By.css选择器 两者都不起作用,因为它无法识别元素。有人能帮我吗。如果可能,我们甚至可以单击“上一个”和“下一个”,以便我们也可以单击一些以前的日期吗?

  • 我无法从日历中选择数据,因为它没有可选择的id。 下面是我的HTML代码, 我试过的代码是 但无法选择日期 申请的网址: http://demo.guru99.com/V4/ 用户名: mgr123密码: mgr!23 单击左侧窗格中的新帐户,选择上面讨论的所需页面。 有人能帮忙选择日期吗?

  • 我试着从我的应用程序(ICS及以上)读取本机日历事件,但有时它工作,有时它显示一些不正确的值。目前我正在使用这段代码,谁能告诉我,我哪里出错了…

  • 问题内容: 有人知道使用Java日历从日期中减去X天的简单方法吗? 我还找不到任何函数可以直接从Java日期中减去X天。有人可以指出我正确的方向吗? 问题答案: 从文档中获取: 根据日历的规则,在给定的日历字段中添加或减去指定的时间量。例如,要从日历的当前时间中减去5天,可以通过调用以下方法来实现:

  • 问题内容: 我想在的日期选择器中禁用特定的日期。 我正在使用CSS作为组件。 我要禁用的日期将根据组合中先前值的选择而动态更改。 我相信应该可以,尽管不确定。 我怎样才能做到这一点 ? 问题答案: 我假设您正在使用Angular-UI中的指令。该属性使您可以禁用某些日期(例如,周末)。看到这个笨蛋http://plnkr.co/edit/gGAU0L?p=preview 如果要基于选择动态禁用日期

  • 我可以点击并打开日期选择弹出日历,但无法从中选择一个特定的日期。由于输入字段是只读的,因此通过或给出的输入不被接受为有效输入。