Reputation: 31
I am working on Selenium WebDriver and using Java. I want to select the date range from the date picker showing in below screen.
Currently, I am able to select date picker and change month selection but unable to select date values (i.e - 12/10/2018)
Datepicker selection
Here the Java Code I have written:
driver.findElement(By.linkText("Leave")).click();
driver.findElement(By.linkText("Apply Leave")).click();
Thread.sleep(2000);
driver.findElement(By.id("LeaveStartDate")).click();
driver.findElement(By.className("next")).click();
driver.findElement(By.className("day")).sendKeys("12");
driver.findElement(By.id("LeaveEndtDate")).click();
driver.findElement(By.className("next")).click();
driver.findElement(By.className("day")).sendKeys("12/11/2018");
}
}
Upvotes: 2
Views: 14376
Reputation: 3
you can try this utility...
public void selectDate(String date){ //Here any date you can give
WebElement eval=driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody"));
List<WebElement> alldates = eval.findElements(By.tagName("td"));
for(WebElement cell:alldates){
String day=cell.getText();
if (cell.getText().contains(date)) {
driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody/tr/td[text()='"+day+"']")).click();
break;
}
}
}
Note: If you want to choose date from another month just navigate to that month and after this you can use this utility.
Upvotes: 0
Reputation: 1
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class dateTimePicker {
WebDriver driver;
@Test
public void dateTimePicker() {
System.setProperty("webdriver.chrome.driver",
"C://Chrome driver exe path");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://jqueryui.com/resources/demos/datepicker/other-months.html");
// click to open the date time picker calendar.
WebElement dateBox = driver.findElement(By.id("datepicker"));
// click to open the date time picker calendar.
dateBox.click();
// Fill date as mm/dd/yyyy as 10/6/2019
dateBox.sendKeys("10/6/2019");
// Press tab to shift focus to time field
dateBox.sendKeys(Keys.TAB);
// close the driver
driver.close();
}
}
Upvotes: 0
Reputation: 31
Thank you guys for responded to my query... I have tried with Xpath and its works for me
Here the details:
With Below code i have been selecting Start & End date in "Date Picker"
driver.findElement(By.id("LeaveStartDate")).click();
driver.findElement(By.className("next")).click();
driver.findElement(By.xpath("html/body/div[8]/div[1]/table/tbody/tr[3]/td[2]")).click();
driver.findElement(By.id("LeaveSEndDate")).click();
driver.findElement(By.xpath("html/body/div[9]/div[1]/table/tbody/tr[3]/td[5]")).click();
Conclusion: Xpath is the solution for those who are unable to execute their selenium Java code of "Jquery Date Picker".
Upvotes: 1
Reputation: 36
to select date after changing month, You can create a List and store all date in list. Then get the text one by one using ".getText()" and try to compare the result date with your expected date and click on it. This way you can parameterize your script with date value.
Upvotes: 0
Reputation: 1736
You need to locate the box that says "Start Date" and sendKeys to that element, rather than to the datepicker itself.
driver.findElement(By.id("LeaveStartDate")).sendKeys("12/10/2018");
Then do the same for the End Date.
Upvotes: 1