0
votes

I have code something like this

WebDriver driver = new FirefoxDriver();
driver.get(something URL);
WebDriverWait waiting = new WebDriverWait(driver, 10, 1000);
WebElement element = waiting.until(ExpectedConditions.presenceOfElementLocated(By.id(my_id)));//problem is here

But then i try to find element on my page, WebDriverWait waiting until the page has completely loaded and then starts searching. If I'm trying something like this

WebDriverWait waiting = new WebDriverWait(driver, 10, 2700);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
WebElement element;
            try {
                driver.get(something_url);
            } catch (TimeoutException e) {
                ((JavascriptExecutor) driver).executeScript("window.stop();");

            }finally {
                element = waiting.until(ExpectedConditions.presenceOfElementLocated(By.id(my_id)));
            }

It works, but if I go on like this

element.click();//go to another page

On this line doesn't throw a timeout exception, I have to wait for the full page load. How to be in this situation ?

1

1 Answers

1
votes

Solution is to wait for ajax refresh to complete on this page:

public void waitForAjaxRefresh() {
       System.out.println("Waiting for Ajax Refresh");
        try {
            WebDriverWait wait = new WebDriverWait(driver,35);
            final JavascriptExecutor javascript = (JavascriptExecutor) (driver instanceof JavascriptExecutor ? driver
                    : null);

            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    boolean outcome = Boolean.parseBoolean(javascript
                            .executeScript("return jQuery.active == 0")
                            .toString());
                    return outcome;
                }
            });
        } catch (TimeoutException ex) {
            throw new TimeoutException("Timed out after "
                    + 35
                    + " seconds while waiting for Ajax to complete.");
        } catch (WebDriverException e) {
            System.out.println("JQuery libraries are not present on page "
                    + driver.getCurrentUrl() + " - "
                    + driver.getTitle());
        }
    }