How to Test a Dynamic Web Page with Selenium WebDriver
  July 31, 2017

A page that has dynamic content has elements of the page that are hidden when you first get to the website. For example, if you were to press a button on a page and it loaded new content, like a pop-up form, without going to another page, that’s a dynamic website. It can also be a user-specific web page that changes day-to-day depending on when it’s accessed, where it’s accessed, user preferences, etc.

Alternatively, some websites are static, which means they have unchanging text files, or information always stays the same while you're on that specific page. However, static web pages are not the ones posing difficulties when testing with Selenium WebDriver, so we’re going to focus on testing dynamic web pages.

Selenium’s Solution

So, what exactly is Selenium's solution for testing dynamic content? Can we put the thread to sleep and then just pick it back up after a hardcoded amount of time? Well, you could, but you'd have failures all over your test results and your manager might be pretty upset come that morning report. Selenium actually has two built-in solutions for testing dynamic loading content that we recommend you should use: the explicit and the implicit wait.

Explicit Wait

For when a locator cannot identify a new element, Selenium comes with an integrated Explicit Wait where you can specify an amount of time to wait for a process before automating a command. This should help give the page or particular object enough time to load and identify the element to interact with.

For example, if you wanted to test a newsletter pop-up you might put an Explicit Wait on the containing div, waiting for it to appear, then filling out an email address and clicking submit. You can see how with all the new trends in our dynamic web design, Explicit Waits are a great answer for complex UI testing.

There are a few different ways to use Explicit Wait to command different actions.

  • presenceOfElementLocated - This defines an expectation of identifying an element at the DOM level, such as recognizing that the button exists on the page.
  • visibilityOfElementLocated - This defines an expectation of identifying an element is present at the DOM level and visible so that the button is there and the user could see it.
  • visibilityOfAllElementsLocated - This checks that all elements are identified and visible, such as a button and an image.
  • elementToBeClickable - This defines an expectation to check for a visible element and click it, such as clicking a button.

There's a few more than that in the ExpectedConditions class, but these are the most basic commands needed to test a dynamic web page. Since most dynamic content can be handled with either elementToBeClickable and presenceOfElementLocated, or a combination, the example will be using those. However, depending on your web application, you can use the previous Explicit Wait commands based on what you need to test.

For now, a practical example of using the Explicit Wait might be:

  import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebDriverWait;

import.ExpectedConditions;

    driver.get(URL);

      WebDriverWait wait = new WebDriverWait(driver, 10);

      wait.until(ExpectedConditions.presenceOfElementLocated(locator));

      wait.until(ExpectedConditions.elementToBeClickable(ID));

      searchID.click();

    public void teardown() {

driver.quit();

   }

}

This command should wait until the new element is identified and return it. If the condition can find the element, it returns it as a result and will move to the next step in the test. If it cannot, the wait will try again until it times out.

The explicit wait should work for both user-specific web pages and sites with hidden elements, so you can test a dynamic web page with Selenium WebDriver.

Implicit Waits

The Implicit Wait is Selenium's other solution, which sets a default wait time if the Selenium script can’t perform the action right away. While some may say you can mix Implicit and Explicit waits, it’s recommended that you don’t because it can cause unpredictable wait times or a timeout. The Implicit Wait will stay set throughout your script, so if you set your Implicit Wait to 10, it will wait 10 seconds to load elements between each command.

Implicit waits are okay for generally slowing down your test execution, something testers may want to do when running tests against a remote device cloud ( allowing time for a certain amount of latency).

Learn more about testing Implicit and Explicit Waits in our Selenium Testing guide for Python and Java.