In this session you will learn how to handle Date pickers
There are different types of date pickers as per the application requirement, here in this session you will be learning two types of mostly used Date pickers
You can download this example program by scrolling to the bottom on the description and also you can import the complete example programs from below GitHub Repository
https://github.com/knowledgeshare-tec...
*******************************************************************
You can see step by step selenium sessions from below playlist
*******************************************************************
• How to handle WebTables using Seleniu...
**********************************************
Watch Xpath Session here - Part - 1
**********************************************
• Introduction to XPath and How to Iden...
**********************************************
Watch Xpath Session here - Part - 2
**********************************************
• How to Identify Elements using XPath ...
**********************************************
Watch Xpath Session here - Part - 3
**********************************************
• Selenium-XPath-Axes - following,follo...
***********************************************
For regular updates you can join this Group
***********************************************
/ 4754296501308288
****************************************************
Overview on Date Pickers
****************************************************
Key to work with Date Pickers
XPath
ListLTWebElementGT
Strings
Where Data Pickers are used ?
Flight Booking Websites
Banking applications, Healthcare applications, etc.,
***************************************************************
Example - 1 - program for Handling Date Picker
***************************************************************
package com.seleniumbasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DatePicker_Example1 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("https://phptravels.net/home");
driver.manage().window().maximize();
String Month_to_book="December,";
String day_to_book ="20";
driver.findElement(By.xpath("//*[@id=\"checkin\"]")).click();
/* Finding the Month */
while(true)
{
String month_displayed=driver.findElement(By.xpath("//*[@id=\"datepickers-container\"]/div[1]/nav/div[2]")).getText();
System.out.println("Month dsiplayed is : " + month_displayed); if(month_displayed.contains(Month_to_book))
{break;
}
else
{driver.findElement(By.xpath("//*[@id=\"datepickers-container\"]/div[1]/nav/div[3]")).click();
Thread.sleep(3000);
}
}
/* Clicking on desired Date */
driver.findElement(By.xpath("//*[@id=\"datepickers-container\"]/div[1]/div/div/div/div[contains(text()," + day_to_book + ")]")).click();
}
}
***************************************************************
Example - 2 - program for Handling Date Picker
***************************************************************
package com.seleniumbasics;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class DatePickerExample2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("https://www.cleartrip.com/");
String Month_to_book="December 2020";
String day_to_book="20";
driver.findElement(By.xpath("//*[@id=\"DepartDate\"]")).click();
while (true)
{
String Month_displayed=driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/div/div/span[1]")).getText();
System.out.println("Month displayed : " + Month_displayed);
String Year_displayed=driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/div/div/span[2]")).getText();
System.out.println("Year displayed : " + Year_displayed);
String final_month_year=Month_displayed + " " +Year_displayed;
if(final_month_year.equalsIgnoreCase(Month_to_book))
{break;
}
else
{
driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[2]/div/a")).click();
}
}
Thread.sleep(3000);
//Method - 1 -- Using Variable
//driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/table/tbody/tr/td/a[contains(text()," + day_to_book + ")]")).click();
//Method 2 -- Using List Collection
ListLTWebElementGT Dates=driver.findElements(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/table/tbody/tr/td/a"));
for(WebElement ele:Dates)
{
if(ele.getText().equalsIgnoreCase(day_to_book))
{
ele.click();
}
} }
}