Reputation: 9
How can I handle the calendar element with Selenium WebDriver with Java? I want to select the date which is the current date + 5 days. Please suggest to me how I can do this. I saw that the calendar contains a webtable.
Upvotes: 0
Views: 1544
Reputation: 371
First Add 5 days in date. Then extract only integer value of date and integer value of month. Once you have these values you can easily select date.
Use below code
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
int month = c.get(Calendar.MONTH)+ 1;
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println("Month : " + month + " day : " + day);
Upvotes: 1
Reputation: 3384
Assuming ur date picker calendar is in table format you can use:
public void checkDate(){
String currentDate = null;
int counter=0;
WebElement tab = driver.findElement(By.id("tabid"));
List<WebElement> rows= tab.findElements(By.tagName("tr"));
for(int i =0;i<=rows.size()-1;i++){
List<WebElement> columns=rows.get(i).findElements(By.tagName("td"));
Iterator itr = columns.iterator();
while(itr.hasNext()){
WebElement we=(WebElement) itr.next();
if(we.getText().equals(currentDate)){
break;
}
counter=counter+1;
}
//element to be clicked is +5 to current date
if(driver.findElement(By.cssSelector("tr:nth-child(i) li:nth-child(counter+5)")).isDisplayed()){
driver.findElement(By.cssSelector("tr:nth-child(i) li:nth-child(counter+5)")).click();
}
}
PS. This should serve your purpose but you need to handle the case when your current date is in last column of the table.
Upvotes: 1