public WebElement waitForElement(By by) {
        int attempts = 0;
        int size = driver.findElements(by).size();

        while (size == 0) {
            size = driver.findElements(by).size();
            if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
                                                             by.toString(),
                                                             MAX_ATTEMPTS));
            attempts++;
            try {
                Thread.sleep(1000); // sleep for 1 second.
            } catch (Exception x) {
                fail("Failed due to an exception during Thread.sleep!");
                x.printStackTrace();
            }
        }

        if (size > 1) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

        return driver.findElement(by);
    }
public WebElement waitForElement ( By by ) { int attempts = 0 ; int size = driver . findElements ( by ) . size ( ) ; while ( size == 0 ) { size = driver . findElements ( by ) . size ( ) ; if ( attempts == MAX_ATTEMPTS ) fail ( String . format ( "Could not find %s after %d seconds" , by . toString ( ) , MAX_ATTEMPTS ) ) ; attempts ++ ; try { Thread . sleep ( 1000 ) ; } catch ( Exception x ) { fail ( "Failed due to an exception during Thread.sleep!" ) ; x . printStackTrace ( ) ; } } if ( size > 1 ) System . err . println ( "WARN: There are more than 1 " + by . toString ( ) + " 's!" ) ; return driver . findElement ( by ) ; }
Method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.