0

I have several tests that run with Selenium and HtmlUnitDriver. Sometimes when I run them and want to click on an element or read text, the elements can not be found. Every time a Exception is thrown I save the Code of the Page for debugging purposes. But when I check the Code, the element is there and when I rerun the test everything works fine. My guess is that the page was not completely loaded when I try to access the element. So I would like to wait until Selenium has finished loading the page before I try to access elements.

I fond two ways to achieve it:

  1. Execute Javascript (e.g. window.initComplete) and wait for the result to be true. The problem: In Selenium I have to have an instance of JavascriptExecuter but HtmlUnitDriver is not derived from that class and I cannot switch to FirefoxDriver (which implements the JavascriptExecuter interface) because we are running the tests headless.

  2. Wait for the last element on the page to load The problem with this approach is that the page is based on our framework and if that changes and suddenly there are different elements on the bottom of the page I have to adapt every test.

Any suggestions on how to approach the problem?

Yeti
  • 1,108
  • 19
  • 28
fuzzi
  • 87
  • 8
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – undetected Selenium May 07 '18 at 11:37

1 Answers1

1

You can create a custom ExpectedCondition:

public static ExpectedCondition<Boolean> waitForLoad() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
        }
    };
}
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154