Selecting a date from Date picker using Selenium Web Driver
Calendars look pretty and of
course they are fancy too. So now a days most of the websites are using
advanced jQuery Date pickers instead of displaying individual dropdowns for month, day, year
If we look at the Date picker, it is just
a like a table with set of rows and columns.To select a date ,we just have to
navigate to the cell where our desired date is present.
Here is a sample code on how to pick a 13th date from the next month.
Here is a sample code on how to pick a 13th date from the next month.
package
Others;
import
java.util.List;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.testng.annotations.AfterClass;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.Test;
public class
dropdown {
    public
WebDriver driver;
    @Test
    public void
testSelectBirthMonth() {        
              driver.switchTo().frame(0);  
              driver.manage().timeouts().implicitlyWait(60,
TimeUnit.SECONDS); 
              //Click on textbox so that
datepicker will come  
              driver.findElement(By.id("datepicker")).click();  
              driver.manage().timeouts().implicitlyWait(60,
TimeUnit.SECONDS); 
              //Click on next so that we
will be in next month  
              driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).click();  
              /*DatePicker is a table.So
navigate to each cell   
               * If a particular cell matches value 13 then
select it  
               */  
              WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div"));  
              List<WebElement> rows=dateWidget.findElements(By.tagName("tr"));  
              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;  
                           }  
                          }                           
    }
    @BeforeClass
    public void
beforeClass() throws Exception {
            System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");    
                        driver = new
ChromeDriver();
        driver.get("http://jqueryui.com/datepicker/");        
    }
    @AfterClass
    public void
afterClass() {
       driver.quit();
    }
}

 
No comments:
Post a Comment