In my application, after button click from one page context, the page actually will be rendered/loaded twice: 1) from url specified by button or form 2) page redirection from server side by HTTP 304, in this case, unless I have current thread sleep from several seconds, the element query will throw NullPointException. Here is the scenario. I am just wondering if there is anyway sleep() can be avoided, like event handler or something. I see there is onload in DocumentListener, could it possible to have unload in WindowListener or something?
try (Page page = browserEngine.navigate(LOGIN_URL)) {
element.click();
// 1. page location is login_url
// 2. page location is redirected to account overview
TimeUnit.SECONDS.sleep(3).
Document doc = page.getDocument();
doc.query("something").get();
}