Rajesh S
Rajesh S

Reputation: 191

WebDriver is unable to locate elements. But it locates when i directly access the webpage

Automation: Booking bus ticket
Description:

  1. WebDriver is unable to locate the elements when i enter into the webpage (passengerDetails)

  2. But when i access that page (passengerDetails) directly, it locates the elements.

    Note: Accessing SearchBus->SelectBus->SelectSeat->PassengerDetails - Unable to locate..
    Accessing directly PassengerDetails -Able to Locate.. I guess, this is since the URL changes from http to https. Anyhow suggest me a solution for this..

Upvotes: 0

Views: 490

Answers (2)

HemChe
HemChe

Reputation: 2337

Find Below the code for your requirement. It worked for me till the point of entering passenger details. Let me know if this was not you were looking for.

package org.com.selen;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class JetBus {

    protected static WebDriver driver;


    public static void main(String[] args) {

        System.setProperty("webdriver.ie.driver", "E:\\Books&Tutorials\\Selenium\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        driver.get("www.jetbus.in");
        driver.findElement(By.id("o_source")).clear();
        driver.findElement(By.id("o_source")).sendKeys("Bangalore");
        driver.findElement(By.id("o_dest")).clear();
        driver.findElement(By.id("o_dest")).sendKeys("Hyderabad");
        driver.findElement(By.id("departDate")).click();
        driver.findElement(By.linkText("11")).click();
        driver.findElement(By.id("search")).click();
        driver.findElement(By.xpath("//img[contains(@src,'http://jetbus.in/images/viewseats.png')]")).click();

//We need to switch to the fancybox frame before performing next set of operations

driver.switchTo().frame("fancybox-frame");
        driver.findElement(By.id("availableSeatImgId33")).click();
        new Select(driver.findElement(By.id("boardingPoint"))).selectByVisibleText("Hebbal - 11:05 pm");
        driver.findElement(By.cssSelector("option[value=\"122989^11:05 pm\"]")).click();
        driver.findElement(By.id("continue")).click();

//After you click Continue, it directly returns to passenger details page.

new Select(driver.findElement(By.id("i_passengerGender"))).selectByVisibleText("Mr");
        driver.findElement(By.id("i_passengerName")).clear();
        driver.findElement(By.id("i_passengerName")).sendKeys("JetBusPassenger");
        driver.findElement(By.id("i_passengerAge")).clear();
        driver.findElement(By.id("i_passengerAge")).sendKeys("49");
        driver.findElement(By.id("i_passengerMobile")).clear();
        driver.findElement(By.id("i_passengerMobile")).sendKeys("3698521463");
        driver.findElement(By.id("i_passengerEmail")).clear();
        driver.findElement(By.id("i_passengerEmail")).sendKeys("[email protected]");
        driver.findElement(By.name("ContinuePayNew")).click();
        driver.findElement(By.cssSelector("div.notAvailableDiv > a > span")).click();


    }

}

Upvotes: 0

buddy
buddy

Reputation: 183

So your site is definitely using iframe type. Basically to make your code work, you need to switch back from "iframe" to your site. try this code

driver.switchTo().defaultContent();

Upvotes: 1

Related Questions