如何从日历中硒使用Java自动化特定日期的选择 [英] How to automate selection of a particular date from calendar in selenium using java

查看:248
本文介绍了如何从日历中硒使用Java自动化特定日期的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这我要挑3天回日期从calendar.How使用selenium.I正在使用的Java硒自动化..自动这种情况下,情况

I have a case in which I have to pick 3 days back date from the calendar.How to automate this case using selenium.I am using java with selenium for automation..

推荐答案

1)假设是,你可以写在输入字段的日期和日历只是图标。你可以有辅助方法,这样的事情

1) Assumption is that you can write the date in the input field and calendar is only the icon. You can have helper method something like this

    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;
}

和后来在code

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

2)如果你只能按一下日历,这将是多一点棘手。你仍然需要字符串,但稍有不同:

2) If you can only click the calendar, It would be little more tricky. You still need the String, but little different:

    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这可能是因为PTED 29.4间$ P $。你不希望发生的事情。因此,在code以后你可能不得不这样做。

But the above has little catch. If the date is 1.4. then it will return you "29" which could be interpreted as 29.4. which you dont want to happen. So later in the code you will probably have to do this

//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();

这篇关于如何从日历中硒使用Java自动化特定日期的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆