I am currently learning Selenium, and I learned a lot. One thing the community said; is that you need avoiding thread.sleep as much as possible. Selenium uses implicit and explicit waits in replace. Yes, I understand that concept.
Recently I cam across a problem. This is that without a certain action; going from the login page to another page, without the use of a Thread.sleep(1000). Selenium seems too crash: that it can't find a certain element. I find this behaviour strange. So I was thinking that this conflict occurs, because of the login page that firstly wants to redirects to the main page of the website and without the Thread.sleep(1000); it wants to go to the second page but the login page refuses it because it want's to go first to the main page. With that being said is that why Selenium crashes or do you guys see and strange use of code in the example below?
// Currently on a webpage
WebElement ui_login_button = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("account-login-button")));
ui_login_button.click();
//After the click it logs in and redirects to a webpage
Thread.sleep(1000); // why sleep here? (without this Selenium crashes)
// Go to second page and perform actions
waitForLoad(driver);
driver.navigate().to(URL + "/mymp/verkopen/index.html");
/* -------------------------------------------------------------------
public void waitForLoad(WebDriver driver) {
ExpectedCondition<Boolean> pageLoadCondition = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
//WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(pageLoadCondition);
}
Sorry for the explanation, I tried my best to be clear. English is not my native language. Thanks for your help.
Kind regargds.