Chetan
Chetan

Reputation: 3

Selenium - WebDriver not recognizing elements but IDE recognizes same elements

I am working on a sample assignment on Thomson Holidays website (http://www.thomson.co.uk/holidays.html). On left hand side there is a Holiday Search panel. I am unable to recognize any of these elements in WebDriver. However, in IDE these elements are recognized. Need more info on this as it is the first time i am experiencing such an issue. Below is the code sample:

WebDriver driver = new FirefoxDriver();     
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");

driver.findElement(By.id("searchbutton")).click();


driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();

Thread.sleep(5000);

Upvotes: 0

Views: 4065

Answers (2)

HemChe
HemChe

Reputation: 2337

The search panel is inside an iframe. As the iframe is the first iframe, you can use the below code.

    driver.get("http://www.thomson.co.uk/holidays.html");

    // switch to search frame
    driver.switchTo().frame(0);

    driver.findElement(By.id("searchbutton")).click();
    driver.findElement(By.id("holidayAttribute_1")).click();
    driver.findElement(By.id("holidayAttribute_2")).click();
    driver.findElement(By.id("holidayAttribute_3")).click();

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32855

Because they're in the iframe, you need switch to the iframe first.

Two lines added to your existing code as follows:

WebDriver driver = new FirefoxDriver();     
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");

// optional, unnecessary in your case
// driver.switchTo().defaultContent(); // make sure outside of all iframes

// switch to search frame
WebElement searchFrame = driver.findElement(By.cssSelector("iframe[src='/thomson/page/byo/search/usp.page']"));
driver.switchTo().frame(searchFrame);

driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();

Upvotes: 6

Related Questions