I had this problem with interruptions. Unbeknownst to me, BackboneJS ran on the page and replaced the element I was trying to click. My code was as follows.
driver.findElement(By.id("checkoutLink")).click();
Which, of course, functionally coincides with this.
WebElement checkoutLink = driver.findElement(By.id("checkoutLink")); checkoutLink.click();
Sometimes it happens that javascript would replace the checkoutLink element between search and click, i.e.
WebElement checkoutLink = driver.findElement(By.id("checkoutLink"));
This rightfully threw a StaleElementReferenceException when trying to click a link. I could not find a reliable way to tell WebDriver to wait for javascript to finish, so this is how I ended up solving it.
new WebDriverWait(driver, timeout) .ignoring(StaleElementReferenceException.class) .until(new Predicate<WebDriver>() { @Override public boolean apply(@Nullable WebDriver driver) { driver.findElement(By.id("checkoutLink")).click(); return true; } });
This code will constantly try to click the link, ignoring the values ββof StaleElementReferenceExceptions until a click is reached or a timeout is reached. I like this solution because it eliminates the need to write repeat logic and uses only the built-in WebDriver constructs.
Kenny Aug 24 '14 at 9:34 2014-08-24 09:34
source share