In a bus booking site www.redbus.in I have to select the Date of Journey...
How it will be done using Selenium WebDriver?
In a bus booking site www.redbus.in I have to select the Date of Journey...
How it will be done using Selenium WebDriver?
Just give a try with below xpath
String month="Sept";
String date="28";
"//td[text()='"+month+"']/../..//a[text()='"+date+"']"
It will select Sept,28
Give the month & date based on your requirement.
Below logic is for navigating among months
driver.findElement(By.cssSelector("td.next")).click();
driver.findElement(By.cssSelector("td.previous")).click();
I hope it would work (I didn't try in my machine)
EDIT-I
I've tried below logic in my machine and it is working fine.
driver=new FirefoxDriver();
driver.get("http://www.redbus.in");
//selecting date of journey
driver.findElement(By.id("calendar")).click(); driver.findElement(By.xpath("//td[text()='Sept']/../..//a[text()='27']")).click();
//selecting return jouney
driver.findElement(By.id("calendar1")).click(); driver.findElement(By.xpath("//td[text()='Oct']/../..//a[text()='3']")).click();
you can send your date or time directly too:
WebElement dateBox = driver.findElement(By.xpath("//form//input[@name='bdaytime']"));
//Fill date as mm/dd/yyyy as 09/25/2013
dateBox.sendKeys("09252013");
//Press tab to shift focus to time field
dateBox.sendKeys(Keys.TAB);
//Fill time as 02:45 PM
dateBox.sendKeys("0245PM");
Below is the screenshot of the date picker field that we use on our portal and below it is the code we use to select the highlighted date on the date picker.
if (!TestHelpers.IsElementPresent(By.Id(""), timeout, driver, verificationErrors))
return false;
if (driver.FindElement(By.Id("")).Enabled)
{
if (driver.FindElement(By.Id("")).Text == "")
{
driver.FindElement(By.Id("")).Click();
if (!TestHelpers.IsElementPresent(By.CssSelector("table.ui-datepicker-calendar > tbody > tr > td.ui-datepicker-days-cell-over.ui-datepicker-today > a.ui-state-default.ui-state-highlight"), timeout, driver, verificationErrors))
return false;
driver.FindElement(By.CssSelector("table.ui-datepicker-calendar > tbody > tr > td.ui-datepicker-days-cell-over.ui-datepicker-today > a.ui-state-default.ui-state-highlight")).Click();
if (driver.FindElement(By.Id("")).TagName == string.Empty)
return false;
}
}
There are multiples ways to do that in Selenium. Some web pages provides an option to select Years and Months like we have in windows. Some web pages like REDBUS.in have only Next and Previous buttons to select year and months. I am writing a robust but general method which will be applicable to all types of Calendar event.
You might have to apply your logic for specific requirements but this logic to get dates will always work.