Reputation: 291
How can I select date from a calendar popup like this gender (i.e 24/04/2015 from calendar) using Selenium WebDriver with Java?
I have tried this:
package com.Automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CalendarPopup {
/**
* @param args
*/
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yatra.com/");
driver.findElement(By.id("//div[2]/ul[3]/li[1]/i")).click();
driver.findElement(By.id("a_2015_4_25")).click();
}
}
Upvotes: 3
Views: 10345
Reputation: 5150
You can click a day by selecting it from its id and then click on it
driver.findElement(By.id('a_2015_4_24')).click(); //use this format a_yyyy_m_d
you can also go back or forward by clicking the calendar arrows:
driver.findElement(By.className('js_btnNext')).click() // click the "next" arrow
driver.findElement(By.className('js_btnPrev')).click() // click the "prev" arrow
note that you cannot click past days or days that are not visible, also the calendar must be visible when you click the day.
EDIT: your are selecting wrongly your elements in your code, as you are selecting an element by id passing an xpath to the function, it should be like this:
//....
driver.findElement(By.xpath("//div[2]/ul[3]/li[1]/i")).click();
driver.findElement(By.id('a_2015_4_24')).click();
//...
Upvotes: 3