Using explicit waits
The recommended solution for waiting problems is to use explicit waits. There is already a class full of precanned examples called ExpectedConditions
to make your life easy, and it really is not that hard to use them. You can do the simple things, such as finding an element once it becomes visible in two lines of code:
WebDriverWait wait = new WebDriverWait(getDriver(), 15, 100); WebElement myElement = wait.until(ExpectedConditions. visibilityOfElementLocated(By.id("foo")));
Bear in mind that the ExpectedConditions
class are prime examples. While being helpful, they are really designed to show you how to set explicit waits up so that you can easily create your own. With these examples, it is trivial to create a new class with conditions that you care about in it, which can be reused again and again in your project.
Earlier, we said we would look at a way to work out if your site had finished processing AJAX requests; let's do that now. First of all, we will create a new...