简体   繁体   中英

Handle calendar using Selenium WebDriver

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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