Can't Select random months dates using jquery calender



  • I have Jquery calender in my application. I have task to select dates from different months using Selenium, but I can do only previous month and forward month. Actually I need to select dates from random months by specifying the date and month as input. If there is a way to achieve it let me know.

    **My code for Selecting Previous Month date.**
    
    public static void Date_Picker(String datetoselect,String Xpath,String month) {
            // Click on the Given Calender Button
            driver.findElement(By.xpath(Xpath)).click();
    
            driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/a[1]")).click();
    
            WebElement Month = driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div[@class='ui-datepicker-title']/span"));
            String Month_Message = Month.getText();
    
            if(Month_Message.contains(month))
            {
                WebElement dateWidget = driver.findElement(By.className("ui-datepicker-calendar"));
    
                  List<WebElement> rows=dateWidget.findElements(By.tagName("tr"));
    
                  List<WebElement> columns=dateWidget.findElements(By.tagName("td"));
    
                  for (WebElement cell: columns){
    
                       if (cell.getText().equals(datetoselect)){
    
                       cell.findElement(By.linkText(datetoselect)).click();
                       break;
    
                       }
                    }
            }   
    


  • I would use a script based on the jQuery datepicker api that can also handle different date formats with an execute script method.

    The script should be something like:

    jQuery('datepickerSelector').datepicker( 'setDate', '-1y' );
    

    This will select the current date -1 year no matter the date format. You can adapt it for months and use a random generator and have from -1m to -12m.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2