Calendars look pretty and of course they are fancy too.So now a days most of the websites are using advanced j Query Date pickers instead of displaying individual dropdowns for month,day,year.
If we look at the Datepicker, 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.
Source - mythoughts
If we look at the Datepicker, 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.
- import java.util.List;
- 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.firefox.FirefoxDriver;
- import org.testng.annotations.BeforeTest;
- import org.testng.annotations.Test;;
- public class DatePicker {
- WebDriver driver;
- @BeforeTest
- public void start(){
- System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
- driver = new FirefoxDriver();
- }
- @Test
- public void Test(){
- driver.get("http://jqueryui.com/datepicker/");
- 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;
- }
- }
- }
- }</webelement></webelement>
Source - mythoughts
Nice post ............
ReplyDeletecan you select a date from scrollable date picker using selenium and let us know please? thanks
ReplyDeleteSunny
could you please share screen shot of data picker ?
DeleteHow do i attach the screenshot here, in the above image on clicking the date tab the calender (square) view is shown right, similarly on clicking the tab a scrollable view for date, month and year is shown. So that we can scroll to select the date, scroll to select month and a different scroll to select year.
DeleteSunny
The scrolling is like in casino how the rollers will roll, from top to bottom but not side wise or in this page you have an ad called "Hit Counter" on you right hand side in middle of page below with some numbers displaying the count, dats the scroll view.
DeleteHere u r facing problem with scrolling not with the date picker ? see the below link and add some condition over there might u will get an solution i hope . http://seleniumtwo.blogspot.com/2013/06/scroll-down-method-in-webdriver.html
DeleteThanks much buddy :)
Deletethank you so much.......
ReplyDelete