Handle bootstrap date picker using selenium java
Select date "4/mar/2021" from the calendar
- Step 1: Open this URL in your chrome browser and run this URL for demo date picker https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html
- Step 2: Copy the xpath of date picker element
xpath = //*[@id="sandbox-container1"]/div/span/i - Step 3: Copy the xpath of element from where we will year and month table
xpath = /html/body/div[3]/div[1]/table/thead/tr[2]/th[2] - Step : 4 Copy the xpaths of year table, month table and date table
Year table xpath = /html/body/div[3]/div[3]/table/tbody
Moth table xpath = /html/body/div[3]/div[3]/table/tbody/
Date table xpath = /html/body/div[3]/div[1]/table/tbody
Sample example Code :
package com.qatips;
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 SelectDate {
static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
// To open chrome browser
System.setProperty("webdriver.chrome.driver", "enter your chromedriver path");
WebDriver driver=new ChromeDriver();
driver.get("https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html");
String sdate = "4/mar/2021";
String date_dd_MM_yyyy[] = (sdate.split(" ")[0]).split("/");
driver.findElement(By.xpath("//*[@id='sandbox-container1']/div/span/i")).click();
driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/thead/tr[2]/th[2]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/thead/tr[2]/th[2]")).click();
// Select a year from the calendar
List<WebElement> yearlist = driver.findElement(By.xpath("/html/body/div[3]/div[3]/table/tbody"))
.findElements(By.tagName("span"));
for (int i = 0; i < yearlist.size(); i++) {
if (yearlist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[2])) {
yearlist.get(i).click();
System.out.println("Year found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Year not found.");
}
}
List<WebElement> monthkist = driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/tbody"))
.findElements(By.tagName("span"));
for (int i = 0; i < monthkist.size(); i++) {
if (monthkist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[1])) {
monthkist.get(i).click();
System.out.println("Month found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Month not found.");
}
}
List<WebElement> datelist = driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/tbody"))
.findElements(By.tagName("td"));
for (int i = 0; i < datelist.size(); i++) {
if (datelist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[0])) {
datelist.get(i).click();
System.out.println("Date found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Date not found.");
}
}
}
}
Output:
Comments
Post a Comment