how to automate date picker

+1 vote
Sep 17, 2020 in Selenium by anonymous
• 410 points
1,809 views
Could you elaborate a little more on your query? Do you want to automate selecting a particular date from the calendar using selenium?

I want to automate this 

2 answers to this question.

+1 vote
Best answer

Try the following:

1) Assumption is that you can write the date in the input field and the calendar is only the icon. You can have a helper method something like this

    public String threeDaysBefore(){
    String threeDaysBefore = "";
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.add(Calendar.DAY_OF_YEAR, -3);
    Date before = cal.getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    threeDaysBefore = formatter.format(before);
    return threeDaysBefore;
}

And later in the code

  WebElement calendarManualInput = driver.findElement...// find the manual input field
  calendarManualInput.sendKeys(threeDaysBefore());

2) If you can only click the calendar, It would be a little more tricky. You still need the String, but a little different:

 public String threeDaysBefore(){
    String threeDaysBefore = "";
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.add(Calendar.DAY_OF_YEAR, -3);
    Date before = cal.getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("dd");
    threeDaysBefore = formatter.format(before);
    return threeDaysBefore;
}

But the above has little catch. If the date is 1.4. then it will return you "29" which could be interpreted as 29.4. which you don't want to happen. So later in the code, you will probably have to do this

//this will click three days before
Date today = new Date();
Date minusThree = new Date();
Calendar now = Calendar.getInstance();
now.setTime(today);
Calendar before = Calendar.getInstance();
before.setTime(minusThree);
before.add(Calendar.DAY_OF_YEAR, -3);
int monthNow = now.get(Calendar.MONTH);
int monthBefore = before.get(Calendar.MONTH);

if (monthBefore < monthNow){
  // click previous month in the calendar tooltip on page
}
WebElement dateToSelect = driver.findElement(By.xpath("//span[text()='"+threeDaysBefore()+"']"));
dateToSelect.click();
answered Sep 18, 2020 by Karan
• 19,610 points

selected Sep 19, 2020 by Jordan
I have used this code but i am able to select only month after that it is not working

// WebElement month=driver.findElement(By.className("h144Z"));

// month.sendKeys("April");

//

// WebElement date=driver.findElement(By.className("h144Z"));

// date.sendKeys("14");

// Thread.sleep(6000);

//

// WebElement year=driver.findElement(By.className("h144Z"));

// year.sendKeys("2020");

// Thread.sleep(6000);
Any specific error that you are getting in the process?
It select month after that it is not working
0 votes

Hey, this worked for me.

 DateFormat dateFormat2 = new SimpleDateFormat("dd"); 
            Date date2 = new Date();

            String today = dateFormat2.format(date2); 

            //find the calendar
            WebElement dateWidget = driver.findElement(By.id("dp-calendar"));  
            List<WebElement> columns=dateWidget.findElements(By.tagName("td"));  

            //comparing the text of cell with today's date and clicking it.
            for (WebElement cell : columns)
            {
               if (cell.getText().equals(today))
               {
                  cell.click();
                  break;
               }
            }
answered Sep 18, 2020 by Pankaj

Related Questions In Selenium

+10 votes
17 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,360 points
198,290 views
0 votes
1 answer

How to create Date Object and Compare Dates?

You can convert pd to date (or ...READ MORE

answered May 4, 2018 in Selenium by Samarpit
• 5,910 points
1,321 views
+1 vote
1 answer
0 votes
2 answers
0 votes
1 answer

How to automate the Instagram search bar with selenium

Hi, please don't search by any placeholder ...READ MORE

answered Mar 9, 2019 in Selenium by Surya
• 970 points
6,178 views
0 votes
1 answer

How to automate Flash using Selenium Webdriver?

Hey Gaurav, to automate Flash using Selenium ...READ MORE

answered May 23, 2019 in Selenium by Gaurav
4,266 views
0 votes
1 answer

How to automate radio button and checkbox on a webpage in Selenium Webdriver?

Hey Yashmita, following code snippet automates the ...READ MORE

answered Jul 23, 2019 in Selenium by Anvi
• 14,150 points
2,373 views
0 votes
1 answer

How to Select date from a datepicker with Selenium Webdriver using Python?

Hi Yashim, you can select date from ...READ MORE

answered Jul 31, 2019 in Selenium by Abha
• 28,140 points

edited Oct 7, 2021 by Sarfaraz 10,969 views
+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
8,396 views
+1 vote
1 answer

How to automate instagram login page using java in selenium?

Try the following: package com.company; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import ...READ MORE

answered Sep 15, 2020 in Selenium by Karan
• 19,610 points
7,110 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP