I am writing Selenium 2 code that searches Google by some criteria, then clicks on one of the links in the results to load this link. These steps are repeated in a loop.
Sometimes everything is ok, but sometimes when Selenium driver clicks on the link, Firefox only redirects back to the results. The requested url was never opened. The error occurs randomly in the Firefox HtmlUnitDriver every single time.
Here is the code:
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://google.com");
//set the search engine criteria
engineSearchInput = driver.findElement(By.xpath("//input[@type='text']"));
engineSearchInput.sendKeys(this.searchCriteria);
//submit to engine
engineSearchInput.submit();
WebElement link = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath("//a[@href='" + searchUrl + "']"));
}
});
link.click();
I found out how to resolve the problem.
After submitting to google I add
Thread.sleep(1500)and everything is OK now.