日期选择器 - selenium webdriver java

时间:2015-05-26 20:28:36

标签: java selenium selenium-webdriver

我正在尝试根据月份和年份创建一种点击特定日期的方法(我希望仅将方法中的日期,月份和年份作为参数传递)。

我也通过互联网搜索,但我没有得到我的查询的解决方案。

以下是我的方法和我的测试样本申请网址。

我使用的网址: http://www.cleartrip.com/

我的方法:

public static WebElement selectDatefromMultiDate(WebDriver driver, String date, String year,String month) throws IOException, InterruptedException {
          driver.findElement(By.id("DepartDate")).click();
            WebElement dates = driver.findElement(By.className("ui-state-default"));
            List<WebElement> day=dates.findElements(By.tagName("a"));

            String  calMonth = driver.findElement(By.className("ui-datepicker-month")).getText();
              System.out.println(calMonth);
            String calYear = driver.findElement(By.className("ui-datepicker-year")).getText();
            System.out.println(calYear);
            for (WebElement cell: day){
                if(calYear.equalsIgnoreCase(year)){
                     if (calMonth.equalsIgnoreCase(month)){
                           if (cell.getText().equals(date)){  
                               cell.findElement(By.linkText(date)).click();
                           }
                     }
                               break;
            }

     }
            return element;
}

2 个答案:

答案 0 :(得分:0)

您实际上并不需要直接操纵日期选择器。您可以直接在input字段中输入日期字符串。它必须采用正确的格式,例如:

WebElement dateInput = driver.findElement(By.id("DepartDate"));
dateInput.sendKeys("Sat, 6 Jun, 2015");

现在,您需要使用create a date from year, month, day传入format the date into the desired date string format个参数和SimpleDateFormat

答案 1 :(得分:0)

此代码可能会有所帮助。

            try{
                      WebElement dateWidget = driver.findElement(By.xpath(OR.getProperty(object))); 
                      List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));  
                      List<WebElement> columns = dateWidget.findElements(By.tagName("td"));  
                      for (WebElement cell: columns){
                           if (cell.getText().equals(data))
                              {
                                  cell.findElement(By.linkText(data)).click();
                      break; 
                      }
                  }
               }catch(Exception e){
        return Constants.KEYWORD_FAIL+" -- Not able to select the date"+e.getMessage(); 
}