Last active
January 6, 2023 22:05
-
-
Save djangofan/5112655 to your computer and use it in GitHub Desktop.
Exercise on safely waiting for unstable web elements with WebDriver.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this method is not effective | |
// an exception throws us out of the loop | |
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 ); | |
} | |
driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS ); | |
staticlogger.info("Finished safe click."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
wait.until(new ExpectedCondition<Boolean>(){ | |
@Override | |
public Boolean apply(WebDriver webDriver) { | |
WebElement element = webDriver.findElement(by); | |
return element != null && element.isDisplayed(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow.. it works awesome ..used method 5