Locating element by id fails in Selenium 2.0 Webdriver

listed in answer

Locating element by id fails in Selenium 2.0 Webdriver
0 votes, 0.00 avg. rating (0% score)

ANSWER:

The best solution to your problem is by making the driver wait until the id element loads in the browser by using the WebDriverWait Object –

new WebDriverWait(driver, 10).until(new ExpectedCondition() 

        public Boolean apply(WebDriver arg0) 

            WebElement element = driver.findElement(By.id("username"));


            return element.isDisplayed();
        
    });

This makes sure that the driver stops checking if the id element has loaded. If it does not load within 10 secs an timedOutException will be thrown.

by Hari Reddy from http://stackoverflow.com/questions/10242082