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

时间:2012-04-12 05:45:03

标签: java selenium automation webdriver

我有一个案例,我必须从日历中选择3天的日期。如何使用selenium自动化这个案例。我使用java和selenium进行自动化..

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

在这里,我向您展示我的官方代码,用于从其官方网站“GitHub”自动化jqueryui日历。

复制粘贴代码并看到它像魅力一样工作:)

如果你愿意,可以投票:)问候Avadh Goyal

public class calendarHanding {

static int targetDay = 4, targetMonth = 6, targetYear = 1993;

static int currenttDate = 0, currenttMonth = 0, currenttYear = 0;

static int jumMonthBy = 0;

static boolean increment = true;

public static void getCurrentDayMonth() {

    Calendar cal = Calendar.getInstance();
    currenttDate = cal.get(Calendar.DAY_OF_MONTH);
    currenttMonth = cal.get(Calendar.MONTH) + 1;
    currenttYear = cal.get(Calendar.YEAR);
}

public static void getTargetDayMonthYear(String dateString) {
    int firstIndex = dateString.indexOf("/");
    int lastIndex = dateString.lastIndexOf("/");

    String day = dateString.substring(0, firstIndex);
    targetDay = Integer.parseInt(day);

    String month = dateString.substring(firstIndex + 1, lastIndex);
    targetMonth = Integer.parseInt(month);

    String year = dateString.substring(lastIndex + 1, dateString.length());
    targetYear = Integer.parseInt(year);

}

public static void calculateToHowManyMonthToJump() {

    if ((targetMonth - currenttMonth) > 0) {
        jumMonthBy = targetMonth - currenttMonth;

    } else {
        jumMonthBy = currenttMonth - targetMonth;
        increment = false;
    }
}

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    String dateToSet = "16/12/2016";

    getCurrentDayMonth();
    System.out.println(currenttDate);
    System.out.println(currenttMonth);
    System.out.println(currenttYear);

    getTargetDayMonthYear(dateToSet);
    System.out.println(targetDay);
    System.out.println(targetMonth);
    System.out.println(targetYear);

    calculateToHowManyMonthToJump();
    System.out.println(jumMonthBy);
    System.out.println(increment);

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\ashutosh.dobhal\\Desktop\\Software\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.navigate().to(
            "https://jqueryui.com/resources/demos/datepicker/default.html");
    driver.manage().window().maximize();
    Thread.sleep(3000);

    driver.findElement(By.xpath("//*[@id='datepicker']")).click();

    for (int i = 0; i < jumMonthBy; i++) {
        if (increment) {
            driver.findElement(
                    By.xpath("//*[@id='ui-datepicker-div']/div/a[2]/span"))
                    .click();
        } else {
            driver.findElement(
                    By.xpath("//*[@id='ui-datepicker-div']/div/a[1]/span"))
                    .click();
        }
        Thread.sleep(1000);

    }

    driver.findElement(By.linkText(Integer.toString(targetDay))).click();

}
}