使用Selenium Webriver从DatePicker中选择日期

时间:2017-08-08 06:22:11

标签: java selenium selenium-webdriver datepicker automated-tests

这是我的代码:

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

我想从“1”到日期“5”(循环)选择日期,以便我可以应用此循环从日期“1”到“当前日期”进行选择。

1 个答案:

答案 0 :(得分:0)

请检查以下代码是否正常工作:

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

}

我解决了我的问题,现在我可以选择从日历到今天的第一个日期的所有日期。