简体   繁体   English

使用Selenium Webriver从DatePicker选择日期

[英]Select Date from DatePicker using Selenium Webriver

Here is my code : 这是我的代码:

for(int i=1;i<=5;i++)
{
    driver.findElement(By.id("fromdate")).click();
    List<WebElement> allDates=driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//td"));

    String ee = String.valueOf(i);
    for(WebElement ele:allDates)
    {
        String date=ele.getText();
        if(date.equalsIgnoreCase(ee)) 
        {
            ele.click();
            Thread.sleep(500);
        }
        System.out.println(i);
        Thread.sleep(500);
    }
} 

I want to select date from "1" to date "5" (By a loop), so that further i can apply this loop to select from date "1" to "current date". 我想选择从“ 1”到“ 5”的日期(按循环),这样我就可以应用该循环从日期“ 1”到“当前日期”进行选择。

Please check below code is working fine : 请检查以下代码是否工作正常:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String a = (dateFormat.format(date));
LocalDate localDate = LocalDate.now();

int dates = localDate.getDayOfMonth();

String t[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" ,"16", "17", "18", "19", "20", "21", "22", "23"," 24", "25", for (int i = 0; i <= dates - 3; i++) {
driver.findElement(By.id("drawdate")).click();
WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<WebElement> allDates = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//td"));

for (WebElement ele : allDates) {
String datee = ele.getText();
if (datee.equalsIgnoreCase(t[i]) && !datee.equalsIgnoreCase(a)) {
ele.click();
Thread.sleep(500);
break;
}

} }

I got the solution of my problem , now i can select all dates from calender till today's date from first date of month. 我已经解决了我的问题,现在我可以选择日历中的所有日期,也可以选择当月的第一天到今天的所有日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM