I am a Software tester who has a very basic knowledge of java and Selenium2. I am trying to write a Junit Selenium2 test to click on an item if it appears on the page.(The majority of the time this item should not appear) if it is not there then i would like the code to just click an alternative item. I have tried using an if else statement. My code is shown below.When i execute the code it just results in an error.(NoSuchElementException) because it can't find the id "house". plwease could someone help.
@Test
public void testMyLoop() throws Exception {
if (driver.findElement(By.id("house")).isDisplayed()) {
driver.findElement(By.id("house")).sendKeys("test");
} else {
driver.findElement(By.linkText("Garage")).click();
}
}