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 e
I am guessing that the exception is raised after you've navigated to URL + "/mymp/verkopen/index.html" and started to take some action.
I am speculating that the main issue here is that your waitForLoad() method is not waiting for some Javascript, or other background task, to complete on the page that Login goes to first. So when you navigate to the next page, something is not yet completed, leaving your user authentication in a bad state. Perhaps you need to wait for some AJAX to complete after login before proceeding with your further navigation? Or it would be better to click on a link on that page to trigger the navigation to your target page (as a real user would), rather than directly entering the URL? You might find it helpful to discuss the actual behavior of the web application with developers.
As DebanjanB has pointed out, once you are on your target page you can then use WebDriverWait for the elements on the page where you are taking actions.
As per your question and the updated comments It raises an exception that it can't find the element on the webpage, it is very much possible. Additionally when you mention putting a sleep in between is not an elegant solution to fix, that's pretty correct as inducing Thread.sleep(1000);
degrades the overall Test Execution Performance.
Now, what I observed in your commented code block to compare document.readyState
to complete
was a wiser step. But sometime it may happen that, though Web Browser will send document.readyState
as complete
to Selenium, due to presence of JavaScript and AJAX Calls the elements with whom we want to interact may not be Visible, Clickable or Interactable which in-turn may raise associated Exception.
So, the solution would be inducing ExplicitWait i.e. WebDriverWait. We will induce ExplicitWait for the element with which we want to interact, with proper ExpectedConditions set. You can find documentation about ExplicitWait here.
If you want to wait for a button to be clickable the expected code block may be in the following format along with the imports:
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// Go to second page and wait for the element
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("id_of_the_element")));
//perform actions
driver.navigate().to(URL + "/mymp/verkopen/index.html");