Automated Testing Elements on a Lazy Load Page with Selenium WebDriver

How to verify elements that get dynamically loaded on a page using Selenium WebDriver

Courtney Zhan

--

Some modern websites now use lazy loading (also known as progressive loading). Instead of loading everything on the page at once, the items load as you scroll. This brings challenges to test automation, because the element we want to control may not be displayed yet. An example site is Substack, if you scroll down on this site, you will notice the bottom of this page reloads more articles.

This article will show you how to use Selenium WebDriver to force lazy-loaded pages to load your desired element, and drive it.

Test Design

  1. Look for the element on the page
  2. If the element is not there, scroll down the page
  3. Repeat steps 1–2

1. Look for the element on the page

First, try to locate the element. In the best case, the element is already there on the initial page load. This means there is nothing to do, and we can proceed normally.

driver.get("https://agileway.substack.com/archive")
driver.find_element(:link_text, "12 Payment Test (AJAX)").click
try_for(4) {…

--

--