How to select the Date Picker In Selenium WebDriver

后端 未结 8 1808
再見小時候
再見小時候 2020-12-03 00:02

Currently working on Selenium WebDriver and using Java. I want to select values in date range from the drop down.. I want to k

相关标签:
8条回答
  • 2020-12-03 00:29

    You can try this, see if it works for you.

    Rather than choosing date from date picker, you can enable the date box using javascript & enter the required date, this would avoid excessive time required to traverse through all date elements till you reach one you require to select.

    Code for from date

    ((JavascriptExecutor)driver).executeScript ("document.getElementById('fromDate').removeAttribute('readonly',0);"); // Enables the from date box
    
    WebElement fromDateBox= driver.findElement(By.id("fromDate"));
    fromDateBox.clear();
    fromDateBox.sendKeys("8-Dec-2014"); //Enter date in required format
    

    Code for to date

    ((JavascriptExecutor)driver).executeScript ("document.getElementById('toDate').removeAttribute('readonly',0);"); // Enables the from date box
    
    WebElement toDateBox= driver.findElement(By.id("toDate"));
    toDateBox.clear();
    toDateBox.sendKeys("15-Dec-2014"); //Enter date in required format
    
    0 讨论(0)
  • 2020-12-03 00:40

    Do not inject javascript. That is a bad practice.

    I would model the DatePicker as an element like textbox / select as shown below.

    For the detailed answer - check here- http://www.testautomationguru.com/selenium-webdriver-automating-custom-controls-datepicker/

    public class DatePicker {
    
        private static final String dateFormat = "dd MMM yyyy";
    
        @FindBy(css = "a.ui-datepicker-prev")
        private WebElement prev;
    
        @FindBy(css = "a.ui-datepicker-next")
        private WebElement next;
    
        @FindBy(css = "div.ui-datepicker-title")
        private WebElement curDate;
    
        @FindBy(css = "a.ui-state-default")
        private List < WebElement > dates;
    
        public void setDate(String date) {
    
            long diff = this.getDateDifferenceInMonths(date);
            int day = this.getDay(date);
    
            WebElement arrow = diff >= 0 ? next : prev;
            diff = Math.abs(diff);
    
            //click the arrows
            for (int i = 0; i < diff; i++)
                arrow.click();
    
            //select the date
            dates.stream()
                .filter(ele - > Integer.parseInt(ele.getText()) == day)
                .findFirst()
                .ifPresent(ele - > ele.click());
    
        }
    
        private int getDay(String date) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
            LocalDate dpToDate = LocalDate.parse(date, dtf);
            return dpToDate.getDayOfMonth();
        }
    
        private long getDateDifferenceInMonths(String date) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
            LocalDate dpCurDate = LocalDate.parse("01 " + this.getCurrentMonthFromDatePicker(), dtf);
            LocalDate dpToDate = LocalDate.parse(date, dtf);
            return YearMonth.from(dpCurDate).until(dpToDate, ChronoUnit.MONTHS);
        }
    
        private String getCurrentMonthFromDatePicker() {
            return this.curDate.getText();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 00:40

    From Java 8 you can use this simple method for picking a random date from the date picker

    List<WebElement> datePickerDays = driver.findElements(By.tagName("td"));
    datePickerDays.stream().filter(e->e.getText().equals(whichDateYouWantToClick)).findFirst().get().click();
    
    0 讨论(0)
  • 2020-12-03 00:42

    DatePicker are not Select element. What your doing in your code is wrong.

    Datepicker are in fact table with set of rows and columns.To select a date you just have to navigate to the cell where our desired date is present.

    So your code should be like this:

    WebElement dateWidget = driver.findElement(your locator);
    List<WebElement> columns=dateWidget.findElements(By.tagName("td"));
    
    for (WebElement cell: columns){
       //Select 13th Date 
       if (cell.getText().equals("13")){
          cell.findElement(By.linkText("13")).click();
          break;
     }
    
    0 讨论(0)
  • 2020-12-03 00:45

    You can directly use following javascript

    ((JavascriptExecutor)driver).executeScript("document.getElementById('fromDate').setAttribute('value','10 Jan 2013')")
    
    0 讨论(0)
  • 2020-12-03 00:47

    I think this could be done in a much simpler way:

    1. Find the locator for the Month (use Firebug/Firepath)
    2. This is probably a Select element, use Selenium to select Month
    3. Do the same for Year
    4. Click by linkText "31" or whatever date you want to click

    So code would look something like this:

    WebElement month = driver.findElement(month combo locator);
    Select monthCombo = new Select(month);
    monthCombo.selectByVisibleText("March");
    
    WebElement year = driver.findElement(year combo locator);
    Select yearCombo = new Select(year);
    yearCombo.selectByVisibleText("2015");
    
    driver.click(By.linkText("31"));
    

    This won't work if the date picker dropdowns are not Select, but most of the ones I've seen are individual elements (select, links, etc.)

    0 讨论(0)
提交回复
热议问题