Question

I am newbie to Selenium and not able to select date website http://www.redbus.in. Can some one help me out? I tried to pass value to the read only text box, but it was in vain.

public static void setup() throws Exception {
    System.out.println("Browser Set up start.. ");
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.get("http://www.redbus.in/");
    System.out.println("Browser Set up Completed ");
}

@Test
public static void SelectSrcDest() throws Exception {
    System.out.println("Constructing Url to open");

    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Chennai");


    driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();
    driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();

    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("window.document.getElementById('txtOnwardCalendar').setAttribute('value','27-Mar-2014')");
    driver.findElement(By.xpath("html/body/div[2]/div/section/div[4]/button")).click();
}

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    setup();
    SelectSrcDest();
}
Was it helpful?

Solution 4

Try to figure out the element through ID.. Its very easy to target exactly by the ID.

driver.findElement(By.id("txtOnwardCalendar")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("monthTable first")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("monthTitle"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select  Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
}

OTHER TIPS

You can use xPath to find the element by text and then use the div id & table class to choose which trip & month to pick.

For instance in order to select the date of journey:

WebDriver driver = new FirefoxDriver();

driver.get("http://www.redbus.in");
driver.findElement(By.id("txtOnwardCalendar")).click();

By locator = By.xpath("//div[@id='rbcal_txtOnwardCalendar']" +
                      "/table[@class='monthTable first']" +
                      "//td[contains(text(), '10')]");
driver.findElement(locator).click();

would select the 10th day of the first month. If you use monthTable last as the class instead, you get 10th day of the second month. And if you change the div id into rbcal_txtReturnCalendar, you can pick a date for the return trip.

You can try the following piece of code. It works perfect

driver.findElement(By.id("DDLSource")).sendKeys("C");
    driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Chennai']")).click();
    driver.findElement(By.id("DDLDestination")).sendKeys("t");
    driver.findElement(By.xpath("//dl[@id = 'dis']//dt[text()='Theni']")).click();
    driver.findElement(By.className("calenImg")).click();
    driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='17']")).click();
    driver.findElement(By.id("calendar1")).click();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='20']")).click();

Try this to select the date:

driver.findElement(By.id("give id")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("ui-monthpicker-div"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select  Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top