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

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

问题描述

我有一个案例,我必须从日历中选择 3 天前的日期.如何使用 selenium 自动化这个案例.我正在使用带有 selenium 的 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;
}

然后在代码中

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

2) 如果你只能点击日历,那就有点棘手了.您仍然需要 String,但几乎没有什么不同:

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",这可以解释为 29.4.你不想发生的.所以稍后在代码中你可能不得不这样做

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天全站免登陆