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.
An Example:
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");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…