0

In a bus booking site www.redbus.in I have to select the Date of Journey...

How it will be done using Selenium WebDriver?

Shyamala
  • 69
  • 4
  • 6
  • 13

4 Answers4

3

Just give a try with below xpath

String month="Sept";
String date="28";
"//td[text()='"+month+"']/../..//a[text()='"+date+"']"

It will select Sept,28 enter image description here

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();

enter image description here

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();
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39
  • It's not working. But I tried with the following code:driver.findElement(By.xpath("td[text()='wd']//a[value=27]")).click();Can you help me out – Shyamala Sep 25 '13 at 16:57
  • Just give a try like below , In your code // missed in xpath. driver.findElement(By.xpath("//td[text()='Sept']/../..//a[text()='27']")).click(); It should select 27th from September month – Santoshsarma Sep 25 '13 at 17:40
  • I've tried & it is working in my machine. See the answer I've added logic which is working in my machine. – Santoshsarma Sep 26 '13 at 05:42
  • Yes its working now....But a small clarification required. But why we are using this in the code: /../.. Why it is not selecting without this? – Shyamala Sep 26 '13 at 06:06
  • In xpath, go to parent from child we need to use /.. See the structure of that calendar in firebug so that u can understand. If my answer is working accept & upvote it. – Santoshsarma Sep 26 '13 at 06:22
0

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");
LoveLovelyJava one
  • 343
  • 1
  • 3
  • 16
0

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.

IMAGE CLICK ME

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;
            }
        }
lypabl
  • 1
0

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.

  1. Get the XPath of Calendar.
  2. Navigate to the desired Year and Month by comparing the current WebElement value with your expected. You can use Text values of Year and Month after you reach Calendar element.
  3. Take all td elements in a List for that Year and Month.
  4. Loop through the element and click when your expected date matches with the element in the List.

You might have to apply your logic for specific requirements but this logic to get dates will always work.

NiNa
  • 111
  • 1
  • 4