How to select the Date Picker In Selenium WebDriver

java, jquery, selenium, selenium-webdriver

Solution

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;
 }

Problem

Currently working on Selenium WebDriver and using Java. I want to select values in `date range` from the drop down.. I want to know how can I select the values as `Date, Month and year` in the date picker drop down. Here is the HTML tag: ``` <dd id="date-element"> <input id="fromDate" class="hasDatepicker" type="text" style="width:57px; padding:3px 1px; font-size:11px;" readonly="readonly" name="fromDate" value="01 Jan 2013"> <input id="toDate" class="hasDatepicker" type="text" style="width:57px; padding:3px 1px; font-size:11px;" readonly="readonly" name="toDate" value="31 Dec 2013"> ``` The below sample code i tried: ``` Log.info("Clicking on From daterange dropdown"); JavascriptExecutor executor8 = (JavascriptExecutor)driver; executor8.executeScript("document.getElementById('fromDate').style.display='block';"); Select select8 = new Select(driver.findElement(By.id("fromDate"))); select8.selectByVisibleText("10 Jan 2013"); Thread.sleep(3000); Log.info("Clicking on To daterange dropdown"); JavascriptExecutor executor10 = (JavascriptExecutor)driver; executor10.executeScript("document.getElementById('toDate').style.display='block';"); Select select10 = new Select(driver.findElement(By.id("toDate"))); select10.selectByVisibleText("31 Dec 2013"); Thread.sleep(3000); ```

Original source