0

I tried to automate but the locators are not visible and the div class is appending dynamically.Can any one help me out??

1
  • Can you show us some of your work please? Commented May 16, 2017 at 11:25

3 Answers 3

3

Friends I automated the bootstrap date picker. I am sharing the code may be this help anyone like me.

   driver.findElement(By.id("datepicker")).click();//to click on thedate picker field
    String date_ent = "17-May-2017";
    String date_ent1[] = date_ent.split("-");
    String shipFDay = date_ent1[0];
    String shipFMonth = date_ent1[1];
    String shipFYear = date_ent1[2];
 String date_pres = driver.findElement(By.xpath("//th[@title='Select Month']")).getText();
 System.out.println(date_pres);
    String dp[] = date_pres.split(" ");
    String month_pres = dp[0];
    String year_pres = dp[1];
    if (year_pres.equals(shipFYear)) {
        driver.findElement(By.xpath("//th[@title='Select Month']")).click();

        driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();
        Thread.sleep(5000);

    } else if (Integer.parseInt(year_pres) > Integer.parseInt(shipFYear)) {
        driver.findElement(By.xpath("//th[@title='Select Month']")).click();

        while (2 > 1) {
            year_pres = driver.findElement(By.xpath("//th[@title='Select Year']")).getText();
            if (year_pres.equalsIgnoreCase(shipFYear)) {

                driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();
                Thread.sleep(5000);
                break;
            }
            driver.findElement(By.xpath("//span[@title='Previous Year']")).click();
        }

    } else {
        driver.findElement(By.xpath("//th[@title='Select Month']")).click();
        while (2 > 1) {
            year_pres = driver.findElement(By.xpath("//th[@title='Select Year']")).getText();
            if (year_pres.equalsIgnoreCase(shipFYear)) {

                driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();

                Thread.sleep(5000);
                break;
            }
            driver.findElement(By.xpath("//span[@title='Next Year']")).click();
        }
    }

    switch (shipFMonth) {
    case "Jan": {
        driver.findElement(By.xpath("//td[@data-day='01/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }

    case "Feb": {
        driver.findElement(By.xpath("//td[@data-day='02/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Mar": {
        driver.findElement(By.xpath("//td[@data-day='03/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Apr": {
        driver.findElement(By.xpath("//td[@data-day='04/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "May": {
        driver.findElement(By.xpath("//td[@data-day='05/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Jun": {
        driver.findElement(By.xpath("//td[@data-day='06/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Jul": {
        driver.findElement(By.xpath("//td[@data-day='07/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Aug": {
        driver.findElement(By.xpath("//td[@data-day='08/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Sep": {
        driver.findElement(By.xpath("//td[@data-day='09/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Oct": {
        driver.findElement(By.xpath("//td[@data-day='10/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Nov": {
        driver.findElement(By.xpath("//td[@data-day='11/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    case "Dec": {
        driver.findElement(By.xpath("//td[@data-day='12/" + shipFDay + "/" + shipFYear + "']")).click();
        System.out.println("Date Selected");
        break;
    }
    default: {
        System.out.println("Please enter the date in the standard format like DD-MMM-yyyy");
     break;
    }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

have you considered calling the "update" method via JS to set the date? => https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#update I was able to set the date via the console using the following command, "$('#sandbox-container div').datepicker('update', '2011-03-05');" => https://uxsolutions.github.io/bootstrap-datepicker/?markup=embedded&format=&weekStart=&startDate=&endDate=&startView=0&minViewMode=0&maxViewMode=4&todayBtn=false&clearBtn=false&language=en&orientation=auto&multidate=&multidateSeparator=&keyboardNavigation=on&forceParse=on#sandbox

With that, you can use the JavascriptExecutor to fire off the command

Comments

0
            public void testDAtePicker() throws Exception {
            //Take a String variable for input
            String sdate = "4/mar/2021";
            
            //Take a String array and split date/month/your
    String date_dd_MM_yyyy[] = (sdate.split(" ")[0]).split("/");
            
            //Click the date picker element to open calendar
    driver.findElement(By.xpath("//*[@id='sandbox-container1']/div/span/i")).click();

            //Click to open year list and month list
    driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/thead/tr[2]/th[2]")).click();
    driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/thead/tr[2]/th[2]")).click();
    
    // Select a year from the calendar
            // Take list of webelemnts and proper tag to get actual values from a main element
    List<WebElement> yearlist = driver.findElement(By.xpath("/html/body/div[3]/div[3]/table/tbody"))
            .findElements(By.tagName("span"));
    for (int i = 0; i < yearlist.size(); i++) {
        if (yearlist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[2])) {
            yearlist.get(i).click();
            System.out.println("Year found successfully..");
            break;
        } else if (i == yearlist.size()) {
            System.out.println("Year not found.");
        }
    }
    
            //Select a month from the calendar
            // Take list of webelemnts and proper tag to get actual values from a main element
    List<WebElement> monthkist = driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/tbody"))
            .findElements(By.tagName("span"));
    for (int i = 0; i < monthkist.size(); i++) {
        if (monthkist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[1])) {
            monthkist.get(i).click();
            System.out.println("Month found successfully..");
            break;
        } else if (i == yearlist.size()) {
            System.out.println("Month not found.");
        }
    }
    
             //Select a date from the calendar
             // Take list of webelemnts and proper tag to get actual values from a main element
    List<WebElement> datelist = driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/tbody"))
            .findElements(By.tagName("td"));
    for (int i = 0; i < datelist.size(); i++) {
        if (datelist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[0])) {
            datelist.get(i).click();
            System.out.println("Date found successfully..");
            break;
        } else if (i == yearlist.size()) {
            System.out.println("Date not found.");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.