Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java从selenium中的日历中自动选择特定日期_Java_Selenium_Automation_Webdriver - Fatal编程技术网

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

如何使用java从selenium中的日历中自动选择特定日期,java,selenium,automation,webdriver,Java,Selenium,Automation,Webdriver,我有一个案例,我必须从日历中选择3天的日期。如何使用selenium自动化此案例。我使用java和selenium实现自动化。1)假设您可以在输入字段中写入日期,日历只是图标。您可以使用类似这样的帮助器方法 public String threeDaysBefore(){ String threeDaysBefore = ""; Date date = new Date(); Calendar cal = Calendar.getInstance(); c

我有一个案例,我必须从日历中选择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) 如果你只能点击日历,那就有点麻烦了。您仍然需要字符串,但没有什么不同:

    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();
//这将在三天前单击
今天日期=新日期();
日期减三=新日期();
Calendar now=Calendar.getInstance();
现在,设定时间(今天);
Calendar before=Calendar.getInstance();
设置时间之前(减三);
添加(日历年的第三天);
int monthNow=now.get(Calendar.MONTH);
int monthBefore=before.get(Calendar.MONTH);
如果(月前<月初){
//在第页的日历工具提示中单击上一个月
}
WebElement dateToSelect=driver.findElement(By.xpath(“//span[text()=”“+threeDaysBefore()+”]);
dateToSelect.click();

在这里,我向您展示了我的原始代码,用于从jqueryui日历的官方网站“”自动运行

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

如果你喜欢,请投票:)问候阿瓦德·戈亚尔

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

}
}
公共类日历{
静态int targetDay=4,targetMonth=6,targetYear=1993;
静态int currenttDate=0,currenttMonth=0,currenttYear=0;
静态int jumMonthBy=0;
静态布尔增量=真;
公共静态无效getCurrentDayMonth(){
Calendar cal=Calendar.getInstance();
currentDate=cal.get(日历日/月日);
currenttMonth=cal.get(日历月)+1;
currenttYear=cal.get(日历年);
}
公共静态void getTargetDayMonthYear(字符串dateString){
int firstIndex=dateString.indexOf(“/”);
int lastIndex=dateString.lastIndexOf(“/”);
String day=dateString.substring(0,firstIndex);
targetDay=Integer.parseInt(天);
String month=dateString.substring(firstIndex+1,lastIndex);
targetMonth=Integer.parseInt(月);
String year=dateString.substring(lastIndex+1,dateString.length());
targetYear=Integer.parseInt(年);
}
公共静态void calculateToHowManyMonthToJump(){
如果((目标月-当前月)>0){
jumMonthBy=目标月-当前月;
}否则{
jumMonthBy=当前的tMonth-目标月;
增量=假;
}
}
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
字符串dateToSet=“16/12/2016”;
getCurrentDayMonth();
系统输出打印LN(当前日期);
系统输出打印项次(当前每月);
系统输出打印项次(currenttYear);
getTargetDayMonthYear(日期设置);
系统输出打印项次(targetDay);
系统输出打印项次(目标月);
System.out.println(targetYear);
计算要跳转的次数();
系统输出打印(jumMonthBy);
系统输出打印项次(增量);
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\ashutosh.dobhal\\Desktop\\Software\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
driver.navigate().to(
"https://jqueryui.com/resources/demos/datepicker/default.html");
driver.manage().window().maximize();
睡眠(3000);
findElement(By.xpath(“//*[@id='datepicker']))。单击();
对于(int i=0;i