Ranjith Rajamohan
Ranjith Rajamohan

Reputation: 1

Selecting a date from a webpage without using date picker

I am trying to select a date from a webpage, a birthdate. When I click on the Calendar icon, a new browser window is opened with Calendar and it shows the current date by default. There is no dropdowns for selecting Month and Year. Month and Year are displayed as labels. (Ex. January 2016). There are 4 links in the page <<, < to navigate back, then >, >> to navigate forward.

Here comes the problem. (May be this is simple enough for others). I want to select the date 11-June-1983. I was able to navigate back to January 1983. But now, I want to go to June 1983 and select the date 11, which is not happening. Below is the code. Please be reminded that I am not using the Date Picker option.

    boolean t2=false;
    for(int i=0;i<50;i++)
    {
        //To check if the year is 1983
        try {
                driver.findElement(By.xpath("//*[contains(text(),'1983')]"));
                try{
                    driver.findElement(By.xpath("//*[contains(text(),'June')]"));
                    t2=true;
                    break;                      
                }
                catch (Exception e)
                {
                    WebElement temp3 = driver.findElement(By.xpath("//img[@src='images/next.gif']"));
                    temp3.click();
                }
            } 
        catch (Exception e) {
            WebElement temp2 = driver.findElement(By.xpath("//img[@src='images/prev_year.gif']"));
            temp2.click();
            }
    }

Upvotes: 0

Views: 72

Answers (1)

John Fisher
John Fisher

Reputation: 181

I'd bargain that your exception-handling is masking a subtle issue so that you're unable to determine the root cause. The reason is because you're catching any exception that occurs (per your catch(Exception e) statements). What if the exception occuring is that the next button cannot be found by xpath as you assume is occurring properly? You'll never know when you're catching Exception. You state that the click is not happening, but what does/doesn't happen? Are there any errors?

I'd suggest re-approaching with handling such as the below (free-form pseudo code) and would wager that your stack trace would then point you in the right direction if issues still exist.

boolean t2=false;
boolean correctYearIsPresent = false;
boolean correctMonthIsPresent = false;
do {

     //Using the plural findElements here because a "no elements" condition
     //will not yield an Exception like the singular method will. The
     //list size will simply be 0 instead. If list is >0, element(s) was located.

     if (driver.findElements(By.xpath("//*[contains(text(),'1983')]")).size > 0)
     {
          correctYearIsPresent = true;
     }
     else 
     {
          driver.findElement(By.xpath("//img[@src='images/prev_year.gif']")).click();
     }
while(false == correctYearIsPresent);

do {
     if (driver.findElements(By.xpath("//*[contains(text(),'June')]")).size > 0)
     {
          correctMonthIsPresent = true;
     }
     else 
     {
          driver.findElement(By.xpath("//img[@src='images/next.gif']")).click();
     }
while(false == correctMonthIsPresent);

Upvotes: 1

Related Questions