Skip to content

Instantly share code, notes, and snippets.

@djangofan
Last active January 6, 2023 22:05

Revisions

  1. @austenjt austenjt revised this gist Feb 5, 2014. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions method7.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout( 30, TimeUnit.SECONDS )
    .pollingEvery( 5, TimeUnit.SECONDS )
    .ignoring( NoSuchElementException.class, StaleElementReferenceException.class );
    // using a customized expected condition
    WebElement foo1 = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
    return driver.findElement(By.id("foo1"));
    }
    });
    // using a built-in expected condition
    WebElement foo2 = wait.until( ExpectedConditions
    .presenceOfElementLocated( By.id("foo2") ) );
    // careful with this next one. it requires visibility attribute on html tag
    WebElement foo3 = wait.until( ExpectedConditions
    .visibilityOfElementLocated( By.id("foo3") ) );
  2. @austenjt austenjt revised this gist Feb 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion method5.java
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ public static WebElement getElementByLocator( final By locator ) {
    while ( (System.currentTimeMillis() - startTime) < 91000 ) {
    LOGGER.info( "Searching for element. Try number " + (tries++) );
    try {
    we = wait.until( ExpectedConditions.visibilityOfElementLocated( locator ) );
    we = wait.until( ExpectedConditions.presenceOfElementLocated( locator ) );
    found = true;
    break;
    } catch ( StaleElementReferenceException e ) {
  3. djangofan revised this gist Dec 26, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion method6.java
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,4 @@
    // create a demonstration of using a Closure instead of the typical inner class
    // pattern for @Override that you see above in method #3
    // pattern for @Override that you see above in method #3

    wait.until( webDriver -> webDriver.findElement( By.id( "foo" ) ) );
  4. djangofan revised this gist Aug 6, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions method6.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    // create a demonstration of using a Closure instead of the typical inner class
    // pattern for @Override that you see above in method #3
  5. djangofan revised this gist Apr 1, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@ public static WebElement getElementByLocator( final By locator ) {
    try {
    we = wait.until( ExpectedConditions.visibilityOfElementLocated( locator ) );
    found = true;
    break;
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
    }
  6. djangofan revised this gist Mar 30, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion method5.java
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ public static WebElement getElementByLocator( final By locator ) {
    while ( (System.currentTimeMillis() - startTime) < 91000 ) {
    LOGGER.info( "Searching for element. Try number " + (tries++) );
    try {
    we = wait.until( ExpectedConditions.visibilityOfElementLocated( locator );
    we = wait.until( ExpectedConditions.visibilityOfElementLocated( locator ) );
    found = true;
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
  7. djangofan revised this gist Mar 30, 2013. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -3,30 +3,30 @@
    // times within the 90 second limit. So, the method will run
    // between 15-90 seconds, depending on the situation of failure.
    public static WebElement getElementByLocator( final By locator ) {
    LOGGER.info( "Get element by locator: " + locator.toString() );
    LOGGER.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(5, TimeUnit.SECONDS)
    .ignoring( StaleElementReferenceException.class ) ;
    int try = 0;
    .ignoring( StaleElementReferenceException.class ) ;
    int tries = 0;
    boolean found = false;
    WebElement we = null;
    while ( (System.currentTimeMillis() - startTime) < 91000 ) {
    LOGGER.info( "Searching for element. Try number " + (try+=1) );
    try {
    WebElement we = wait.until( ExpectedCondition.visibilityOfElementLocated( locator );
    found = true;
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    LOGGER.info( "Searching for element. Try number " + (tries++) );
    try {
    we = wait.until( ExpectedConditions.visibilityOfElementLocated( locator );
    found = true;
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    }
    long endTime = System.currentTimeMillis();
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    if ( found ) {
    LOGGER.info("Found element after waiting for " + totalTime + " milliseconds." );
    return we;
    LOGGER.info("Found element after waiting for " + totalTime + " milliseconds." );
    } else {
    LOGGER.info( "Failed to find element after " + totalTime + " milliseconds." );
    LOGGER.info( "Failed to find element after " + totalTime + " milliseconds." );
    }
    return null;
    return we;
    }
  8. djangofan revised this gist Mar 30, 2013. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -3,22 +3,30 @@
    // times within the 90 second limit. So, the method will run
    // between 15-90 seconds, depending on the situation of failure.
    public static WebElement getElementByLocator( final By locator ) {
    LOGGER.info( "Get element by locator: " + locator.toString() );
    LOGGER.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(5, TimeUnit.SECONDS)
    .ignoring( StaleElementReferenceException.class ) ;
    int try = 0;
    boolean found = false;
    while ( (System.currentTimeMillis() - startTime) < 91000 ) {
    LOGGER.info( "Searching for element..." );
    LOGGER.info( "Searching for element. Try number " + (try+=1) );
    try {
    WebElement we = wait.until( ExpectedCondition.visibilityOfElementLocated( locator );
    found = true;
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    }
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    LOGGER.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    if ( found ) {
    LOGGER.info("Found element after waiting for " + totalTime + " milliseconds." );
    return we;
    } else {
    LOGGER.info( "Failed to find element after " + totalTime + " milliseconds." );
    }
    return null;
    }
  9. djangofan revised this gist Mar 30, 2013. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Another approach, after everything I have learned, that might
    // also be effective. With this method, a wait timeout occurs 3
    // times within the 90 second limit. So, the method will run
    // between 15-90 seconds, depending on the situation of failure.
    public static WebElement getElementByLocator( final By locator ) {
    LOGGER.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(5, TimeUnit.SECONDS)
    .ignoring( StaleElementReferenceException.class ) ;
    while ( (System.currentTimeMillis() - startTime) < 91000 ) {
    LOGGER.info( "Searching for element..." );
    try {
    WebElement we = wait.until( ExpectedCondition.visibilityOfElementLocated( locator );
    } catch ( StaleElementReferenceException e ) {
    LOGGER.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    }
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    LOGGER.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
  10. djangofan revised this gist Mar 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion method4.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // I realized that the try-catch doesn't really need to be within
    // the ExpectedCondition final block. By moving the try-catch outside
    // I would have access to the WebElement returned from findElement().
    // So, I can create my own Boolean expected condition while loop to
    // So, I can create my own Boolean expected condition while I loop to
    // hopefully accomplish a similar thing as method3.
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
  11. djangofan revised this gist Mar 21, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions method2.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    // This is the most effective method I could find for
    // locating elements reliably.
    // This was my breakthrough. My first partially working code.
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
  12. djangofan revised this gist Mar 18, 2013. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -6,28 +6,29 @@
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    driver.manage().timeouts().implicitlyWait( 9, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    int tries = 0;
    while ( unfound && tries < 20 ) {
    tries += 1;
    staticlogger.info("Locating remaining time: " + (180-(9*(tries-1) )) + " seconds." );
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    } catch ( StaleElementReferenceException ser ) {
    staticlogger.info( "ERROR: Stale element. " + locator.toString() );
    unfound = true;
    try {
    Thread.sleep(4000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    } catch ( NoSuchElementException nse ) {
    staticlogger.info( "ERROR: No such element. " + locator.toString() );
    unfound = true;
    } catch ( Exception e ) {
    staticlogger.info( e.getMessage() );
    }
    }
    // and finally the cleanup
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    return we;
    }
    }
  13. djangofan revised this gist Mar 15, 2013. 1 changed file with 28 additions and 28 deletions.
    56 changes: 28 additions & 28 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -3,31 +3,31 @@
    // I would have access to the WebElement returned from findElement().
    // So, I can create my own Boolean expected condition while loop to
    // hopefully accomplish a similar thing as method3.
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    unfound = true;
    try {
    Thread.sleep(4000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    }
    // and finally the cleanup
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    return we;
    }
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    unfound = true;
    try {
    Thread.sleep(4000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    }
    // and finally the cleanup
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    return we;
    }
  14. djangofan revised this gist Mar 15, 2013. 1 changed file with 28 additions and 25 deletions.
    53 changes: 28 additions & 25 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -3,28 +3,31 @@
    // I would have access to the WebElement returned from findElement().
    // So, I can create my own Boolean expected condition while loop to
    // hopefully accomplish a similar thing as method3.
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    unfound = true;
    waitSeconds(4);
    }
    }
    }
    // and finally the cleanup
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    unfound = true;
    try {
    Thread.sleep(4000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    }
    // and finally the cleanup
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    return we;
    }
  15. djangofan revised this gist Mar 15, 2013. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    // I realized that the try-catch doesn't really need to be within
    // the ExpectedCondition final block. By moving the try-catch outside
    // I would have access to the WebElement returned from findElement().
    // So, I can create my own Boolean expected condition while loop to
    // hopefully accomplish a similar thing as method3.
    public static WebElement getElementByLocator( By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    WebElement we = null;
    boolean unfound = true;
    while ( unfound ) {
    try {
    we = driver.findElement( locator );
    unfound = false; // FOUND IT
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    unfound = true;
    waitSeconds(4);
    }
    }
    }
    // and finally the cleanup
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
  16. djangofan revised this gist Mar 13, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions method3.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    // an extension of method3 although I am unsure if I trust it
    // because it re-gets the element
    // An extension of method3 although I am unsure if I trust it
    // because it re-gets the element AND the element gets assigned
    // again after returning from the method. A few opportunities
    // to go stale.
    public static WebElement getElementByLocator( final By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
  17. djangofan revised this gist Mar 13, 2013. 5 changed files with 74 additions and 118 deletions.
    39 changes: 16 additions & 23 deletions method1.java
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,16 @@
    // This method is not effective. The while loop wont throw an error but
    // findElements is not guaranteed to return a non-stale element.
    public static WebElement safeGetElementByLocator( By locator ) {
    int weWait = 10;
    int cycle = 1; // 10 cycles is 280
    List<WebElement> weList = driver.findElements( locator );
    while ( weList.size()==0 && weWait <= 280 ) {
    staticlogger.info("DOM not ready. Trying again for " + weWait + " more seconds.");
    staticlogger.info( "[" + cycle + "] Searching for element...");
    driver.manage().timeouts().implicitlyWait( weWait, TimeUnit.SECONDS );
    weList = driver.findElements( locator );
    weWait +=30;
    cycle +=1;
    }
    if ( weList.size() > 1 ) staticlogger.info("WARNING: Locator matched more elements than expected.");
    WebElement we = null;
    try {
    we = weList.get(0);
    } catch ( Exception e ) {
    staticlogger.info( e.getMessage() );
    }
    return we;
    }
    // I gleamed this method from the Selenium Google forum.
    // The .ignoring fluent method was a huge hint
    // to the eventual solution I found but it still didn't work
    // because I needed the ExpectedCondition apply method to give
    // me some kind of message on failures.
    public void waitForElementPresent(final By by, int timeout){
    WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
    .ignoring(StaleElementReferenceException.class);
    wait.until(new ExpectedCondition<Boolean>(){
    @Override
    public Boolean apply(WebDriver webDriver) {
    WebElement element = webDriver.findElement(by);
    return element != null && element.isDisplayed();
    }
    });
    }
    47 changes: 27 additions & 20 deletions method2.java
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,29 @@
    // This method is not effective. I had mixed results with this.
    // I didn't stay with this long enough to determine the actual problem.
    public static void clickByLocator( By locator ) {
    boolean search = true;
    int weWait = 30;
    int cycle = 1;
    while ( search && cycle <= 3 ) {
    try {
    driver.findElement( locator ).click();
    search = false; // stop searching if no error
    } catch ( StaleElementReferenceException sere ) {
    staticlogger.info( "\n\n\nElement was stale. Trying again.\n" + sere.getMessage() + "\n" +
    sere.getCause().getLocalizedMessage() + "\n\n" );
    //sere.printStackTrace();
    }
    weWait +=30; // increase wait value for next cycle
    cycle +=1;
    driver.manage().timeouts().implicitlyWait( weWait, TimeUnit.SECONDS );
    }
    // This is the most effective method I could find for
    // locating elements reliably.
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    //.ignoring( StaleElementReferenceException.class );
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).click();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again...");
    return false;
    }
    }
    } );
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    staticlogger.info("Finished safe click.");
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    }

    44 changes: 31 additions & 13 deletions method3.java
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,34 @@
    // I gleamed this method from the Selenium Google forum.
    // The .ignoring fluent method was a huge hint
    // to the eventual solution I found but it still didn't work
    // because I needed the ExpectedCondition apply method to give
    // me some kind of message on failures.
    public void waitForElementPresent(final By by, int timeout){
    WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
    .ignoring(StaleElementReferenceException.class);
    wait.until(new ExpectedCondition<Boolean>(){
    // an extension of method3 although I am unsure if I trust it
    // because it re-gets the element
    public static WebElement getElementByLocator( final By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver webDriver) {
    WebElement element = webDriver.findElement(by);
    return element != null && element.isDisplayed();
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).getTagName();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    return false;
    }
    }
    });
    } );
    WebElement we = null;
    try {
    we = driver.findElement( locator ); // is this error prone?
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
    29 changes: 0 additions & 29 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    // This is the most effective method I could find for
    // locating elements reliably.
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    //.ignoring( StaleElementReferenceException.class );
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).click();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again...");
    return false;
    }
    }
    } );
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    }

    33 changes: 0 additions & 33 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -1,33 +0,0 @@
    // an extension of method4 although I am unsure if I trust it
    public static WebElement getElementByLocator( final By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).getTagName();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    return false;
    }
    }
    } );
    WebElement we = null;
    try {
    we = driver.findElement( locator ); // is this error prone?
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
  18. djangofan revised this gist Mar 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion method5.java
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ public Boolean apply( WebDriver webDriver ) {
    } );
    WebElement we = null;
    try {
    we = driver.findElement( locator );
    we = driver.findElement( locator ); // is this error prone?
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    }
  19. djangofan revised this gist Mar 13, 2013. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions method5.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // an extension of method4 although I am unsure if I trust it
    public static WebElement getElementByLocator( final By locator ) {
    staticlogger.info( "Get element by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).getTagName();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again for availability of element...");
    return false;
    }
    }
    } );
    WebElement we = null;
    try {
    we = driver.findElement( locator );
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( "Stale element: \n" + e.getMessage() + "\n");
    }
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    return we;
    }
  20. djangofan revised this gist Mar 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions method2.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // This method is not effective.
    // Any exception that occurs will eject us out of the loop.
    // This method is not effective. I had mixed results with this.
    // I didn't stay with this long enough to determine the actual problem.
    public static void clickByLocator( By locator ) {
    boolean search = true;
    int weWait = 30;
  21. djangofan revised this gist Mar 13, 2013. 5 changed files with 10 additions and 10 deletions.
    4 changes: 2 additions & 2 deletions endorsedmethod.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // this is the official Selenium documention endorsed method of waiting for elements
    // this method is ineffective because it still suffers from
    // This is the official Selenium documention endorsed method of waiting for elements.
    // This method is ineffective because it still suffers from
    // the stale element exception.
    public static void clickByLocator ( final By locator ) {
    WebElement myDynamicElement = ( new WebDriverWait(driver, 10))
    4 changes: 2 additions & 2 deletions method1.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // this method is not effective. the while loop wont throw an error but
    // findElements is not guaranteed to return a non-stale element
    // This method is not effective. The while loop wont throw an error but
    // findElements is not guaranteed to return a non-stale element.
    public static WebElement safeGetElementByLocator( By locator ) {
    int weWait = 10;
    int cycle = 1; // 10 cycles is 280
    4 changes: 2 additions & 2 deletions method2.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // this method is not effective
    // an exception throws us out of the loop
    // This method is not effective.
    // Any exception that occurs will eject us out of the loop.
    public static void clickByLocator( By locator ) {
    boolean search = true;
    int weWait = 30;
    4 changes: 2 additions & 2 deletions method3.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // I gleamed this method from the Selenium Google forum
    // notice the .ignoring fluent method. this was a huge hint
    // I gleamed this method from the Selenium Google forum.
    // The .ignoring fluent method was a huge hint
    // to the eventual solution I found but it still didn't work
    // because I needed the ExpectedCondition apply method to give
    // me some kind of message on failures.
    4 changes: 2 additions & 2 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // this is the most effective method I could find for
    // locating elements reliably
    // This is the most effective method I could find for
    // locating elements reliably.
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
  22. djangofan revised this gist Mar 13, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion method3.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    // I gleamed this method from the Selenium Google forum
    // notice the .ignoring fluent method. this is a game changer.
    // notice the .ignoring fluent method. this was a huge hint
    // to the eventual solution I found but it still didn't work
    // because I needed the ExpectedCondition apply method to give
    // me some kind of message on failures.
    public void waitForElementPresent(final By by, int timeout){
    WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
    .ignoring(StaleElementReferenceException.class);
  23. djangofan revised this gist Mar 12, 2013. 1 changed file with 24 additions and 23 deletions.
    47 changes: 24 additions & 23 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,29 @@
    // this is the most effective method I could find for
    // locating elements reliably
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    //.ignoring( StaleElementReferenceException.class );
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).click();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again...");
    return false;
    }
    }
    } );
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    }
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).click();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again...");
    return false;
    }
    }
    } );
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    }

  24. djangofan revised this gist Mar 12, 2013. 1 changed file with 26 additions and 13 deletions.
    39 changes: 26 additions & 13 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,28 @@
    // this is the most effective method I could find for
    // locating elements reliably
    public static void clickByLocator( final By locator ) {
    // uses default implicit timeout defined in test framework
    WebDriverWait wait = (WebDriverWait)new WebDriverWait( driver, 185).ignoring( StaleElementReferenceException.class );
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver webDriver) {
    WebElement element = driver.findElement( locator );
    element.click();
    return element != null && element.isDisplayed();
    }
    } );
    staticlogger.info("Finished safe click.");
    }
    public static void clickByLocator( final By locator ) {
    staticlogger.info( "Click by locator: " + locator.toString() );
    final long startTime = System.currentTimeMillis();
    driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
    .withTimeout(90000, TimeUnit.MILLISECONDS)
    .pollingEvery(5500, TimeUnit.MILLISECONDS);
    //.ignoring( StaleElementReferenceException.class );
    wait.until( new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply( WebDriver webDriver ) {
    try {
    webDriver.findElement( locator ).click();
    return true;
    } catch ( StaleElementReferenceException e ) {
    staticlogger.info( e.getMessage() + "\n");
    staticlogger.info("Trying again...");
    return false;
    }
    }
    } );
    driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    staticlogger.info("Finished click after waiting for " + totalTime + " milliseconds.");
    }
  25. djangofan revised this gist Mar 12, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions endorsedmethod.java
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // this method is ineffective because it still suffers from
    // the stale element exception.
    public static void clickByLocator ( final By locator ) {
    WebElement myDynamicElement = ( new WebDriverWait(driver, 10))
    .until( ExpectedConditions.presenceOfElementLocated( locator ) );
    WebElement myDynamicElement = ( new WebDriverWait(driver, 10))
    .until( ExpectedConditions.presenceOfElementLocated( locator ) );
    myDynamicElement.click();
    }
  26. djangofan revised this gist Mar 12, 2013. 2 changed files with 8 additions and 5 deletions.
    8 changes: 8 additions & 0 deletions endorsedmethod.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    // this is the official Selenium documention endorsed method of waiting for elements
    // this method is ineffective because it still suffers from
    // the stale element exception.
    public static void clickByLocator ( final By locator ) {
    WebElement myDynamicElement = ( new WebDriverWait(driver, 10))
    .until( ExpectedConditions.presenceOfElementLocated( locator ) );
    myDynamicElement.click();
    }
    5 changes: 0 additions & 5 deletions standardmethod.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    // this is the official Selenium documention method of waiting for elements
    // this method is ineffective because it still suffers from
    // the stale element exception.
    WebElement myDynamicElement = ( new WebDriverWait(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
  27. djangofan revised this gist Mar 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion method1.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // this method is not effective
    // this method is not effective. the while loop wont throw an error but
    // findElements is not guaranteed to return a non-stale element
    public static WebElement safeGetElementByLocator( By locator ) {
    int weWait = 10;
  28. djangofan revised this gist Mar 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions method4.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // this is the most effective method I could find for
    // locating elements reliably
    public static void clickByLocator( final By locator ) {
    // uses default implicit timeout defined in test framework
    WebDriverWait wait = (WebDriverWait)new WebDriverWait( driver, 185).ignoring( StaleElementReferenceException.class );
  29. djangofan revised this gist Mar 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions method3.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // I gleamed this method from the Selenium Google forum
    // notice the .ignoring fluent method. this is a game changer.
    public void waitForElementPresent(final By by, int timeout){
    WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
    .ignoring(StaleElementReferenceException.class);
  30. djangofan renamed this gist Mar 12, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.