Monday, September 20, 2010

Handle Datepicker using WebDriver simple sample

Someone asked me: "How to identify a datepicker on the webpage, which will display after clicking the datepicker icon?"
Here is a simple sample, you can customize your own function for reading your web page source and make a api for picking any dates freely :-)

//Find all the Calendar on the Web Page
List<WebElement> Datepickers = driver.findElements(By.className("CalendarIcon"));

//Trigger the calendar of StartDate
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Datepickers.get(0).click();


WebElement table = driver.findElement(By.className("ui-datepicker-calendar"));

List<WebElement> tds = table.findElements(By.tagName("td"));
for (WebElement td: tds){
//Select 20th Date of current month
if (td.getText().equals("20")){
td.findElement(By.linkText("20")).click();
break;
}

}

//Trigger the calendar of EndDate
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Datepickers.get(1).click();

WebElement table2 = driver.findElement(By.className("ui-datepicker-calendar"));

List<WebElement> tds2 = table2.findElements(By.tagName("td"));
for (WebElement td2: tds2){
//Select 22th Date of current month
if (td2.getText().equals("22")){
td2.findElement(By.linkText("22")).click();
break;
}

}

5 comments:

  1. Super let me try

    ReplyDelete
  2. //Find all the Calendar on the Web Page
    List Datepickers = driver.findElements(By.className("CalendarIcon"));


    what does WebElement here actualy mean ?

    ReplyDelete
    Replies
    1. The WebElement means the element on web page have "CalendarIcon" in its class name

      Delete
    2. Hi Cheng,

      I am using the mentioned code but its not working for me,will u plz help...........


      public static void main(String[] args)

      {
      WebDriver driver = new FirefoxDriver();
      //driver.get("http://www.espncricinfo.com/");
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("http://staging.hemscottir.com/ir/template/ir.jsp?page=share-price-calculatorAjax");
      //driver.findElement(By.xpath("//*[@id='inp_date']")).click();
      List Datepickers = driver.findElements(By.xpath("//*[@id='inp_date']"));
      Datepickers.get(0).click();

      //Find all the Calendar on the Web Page
      // List Datepickers = driver.findElements(By.className("CalendarIcon"));

      //Trigger the calendar of StartDate




      /*WebElement table = driver.findElement(By.className("datepickerContainer"));

      List tds = table.findElements(By.tagName("td"));
      for (WebElement td: tds){
      //Select 20th Date of current month
      if (td.getText().equals("20")){
      td.findElement(By.linkText("20")).click();
      break;
      }

      }

      //Trigger the calendar of EndDate
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      Datepickers.get(1).click();

      WebElement table2 = driver.findElement(By.className("datepickerContainer"));

      List tds2 = table2.findElements(By.tagName("td"));
      for (WebElement td2: tds2){
      //Select 22th Date of current month
      if (td2.getText().equals("22")){
      td2.findElement(By.linkText("22")).click();
      break;
      }

      } */
      }

      }

      Delete
  3. Hi i am trying to using link text but value are not working and on my earlier calendar link text was working now what should i do.

    ReplyDelete