Introduction
AJAX (Asynchronous JavaScript and XML) is one of the most commonly used technologies in modern web applications for loading content dynamically without refreshing the entire webpage. Instead of loading all data at once, webpages often retrieve information asynchronously through API calls and update only specific sections of the page.
Because AJAX operates independently of the browser’s initial page load, Selenium may attempt to interact with elements before they become available. Proper synchronization is therefore essential when automating AJAX-based applications.
In this tutorial, you’ll learn what AJAX Synchronization is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.
What is AJAX Synchronization?
AJAX Synchronization is the process of waiting for AJAX requests to complete before Selenium interacts with dynamically loaded web elements.
Instead of using fixed delays, Selenium intelligently waits until:
Loading animations disappear.
Elements become visible.
Elements become clickable.
Text content appears on the webpage.
Dynamic content finishes loading.
This makes automation scripts faster, more reliable, and less prone to synchronization issues.
Why Use AJAX Synchronization?
AJAX Synchronization helps you:
Handle dynamically loaded web content.
Synchronize Selenium with AJAX requests.
Reduce flaky automation tests.
Improve automation reliability.
Avoid unnecessary fixed delays.
Improve execution speed.
Syntax
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.ID, "element_id")
)
)
Where:
WebDriverWaitcreates an Explicit Wait.10represents the maximum timeout in seconds.Expected Conditionsdefine the condition Selenium should wait for.Selenium immediately proceeds once the condition becomes true.
Example
The following example demonstrates AJAX Synchronization using Selenium’s Dynamic Loading page. After clicking the Start button, the webpage displays a loading animation while the content is retrieved asynchronously. Selenium first waits for the loading indicator to disappear and then waits for the Hello World! message to become visible.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Topic: 24. Advanced Synchronization - AJAX Synchronization
# Practice site: https://the-internet.herokuapp.com/dynamic_loading/2
# Run: pytest -s 24_examples/test_01_ajax_synchronization.py
#
# AJAX requests load content asynchronously. Wait for the element that appears
# after the AJAX call completes.
def test_ajax_synchronization():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/dynamic_loading/2")
driver.find_element(By.CSS_SELECTOR, "#start button").click()
WebDriverWait(driver, 10).until(
EC.invisibility_of_element_located((By.ID, "loading"))
)
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR, "#finish h4")
)
)
assert "Hello World!" in driver.find_element(
By.CSS_SELECTOR,
"#finish h4"
).text
finally:
driver.quit()
Output
Hello World!
The loading animation disappeared
successfully and the dynamically
loaded content became visible.
AJAX synchronization completed
successfully.
Understanding the Code
Import Required Modules
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
These modules provide Selenium’s Explicit Wait functionality and predefined Expected Conditions used for synchronization.
Open the Practice Website
driver.get(
"https://the-internet.herokuapp.com/dynamic_loading/2"
)
This page loads content asynchronously after clicking the Start button, making it ideal for demonstrating AJAX Synchronization.
Click the Start Button
driver.find_element(
By.CSS_SELECTOR,
"#start button"
).click()
Clicking the button initiates the AJAX request.
Immediately after clicking:
A loading animation appears.
The Hello World message is not yet available.
Selenium must wait for the AJAX request to complete.
Wait for the Loading Animation to Disappear
WebDriverWait(driver, 10).until(
EC.invisibility_of_element_located(
(By.ID, "loading")
)
)
This step waits until the loading indicator disappears from the webpage.
Selenium repeatedly checks whether:
loading
has become invisible.
Once the loading animation disappears, Selenium proceeds to the next condition.
This approach is commonly used in real-world applications that display:
Loading spinners
Progress indicators
Processing messages
Animated loaders
Wait for the Dynamic Content to Become Visible
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR, "#finish h4")
)
)
After the AJAX request completes, Selenium waits until the dynamically loaded heading becomes visible.
If the element appears before the 10-second timeout, Selenium immediately continues execution.
Validate the Result
assert "Hello World!" in driver.find_element(
By.CSS_SELECTOR,
"#finish h4"
).text
This assertion verifies that:
The AJAX request completed successfully.
The loading animation disappeared.
The dynamically loaded element became visible.
Selenium synchronized correctly with the webpage.
How AJAX Synchronization Works
The following diagram illustrates the execution flow.
Python Script
│
▼
Click Button
│
▼
AJAX Request Starts
│
▼
Loading Animation Appears
│
▼
Wait Until Loading Disappears
│
▼
Wait for Element Visibility
│
▼
Element Visible?
│ │
No Yes
│ │
▼ ▼
Keep Waiting Continue
│
▼
Perform Assertion
│
▼
Test Passes
Practical Example
Suppose an e-commerce website loads product information after an API request.
When the user searches for products:
A loading spinner appears.
Product information is fetched asynchronously.
The products become visible after loading completes.
Instead of using:
time.sleep(10)
Selenium waits until:
The loading spinner disappears.
Product cards become visible.
This significantly improves automation reliability and execution speed.
Automation Testing Example
Consider an online banking application.
After clicking View Transactions:
The server processes the request.
Account information is retrieved.
A loading animation appears.
Transaction details are displayed.
Using AJAX Synchronization, Selenium waits until:
The loading animation disappears.
The transaction table becomes visible.
The required elements become clickable.
The automation script proceeds immediately when the webpage is ready.
Real-World Example
AJAX Synchronization is widely used in:
Banking applications
E-commerce websites
CRM systems
Healthcare portals
Airline booking systems
Government websites
Enterprise web applications
Most modern web applications rely heavily on AJAX-based content loading.
Advantages of AJAX Synchronization
Handles dynamically loaded content efficiently.
Improves automation reliability.
Reduces flaky tests.
Eliminates unnecessary delays.
Improves execution speed.
Provides better synchronization for modern web applications.
Limitations
Requires proper Expected Conditions.
Incorrect timeout values may increase execution time.
Different applications may require different synchronization strategies.
Poor locator strategies can still cause failures.
Common Mistakes Beginners Make
Using time.sleep() Everywhere
Avoid:
import time
time.sleep(10)
Fixed delays unnecessarily increase execution time.
Explicit Wait provides faster and more reliable synchronization.
Waiting for the Wrong Condition
Avoid waiting only for:
presence_of_element_located()
when the element must actually become:
Visible
Clickable
Invisible
Fully loaded
Always choose the Expected Condition that matches the application’s behavior.
Ignoring Loading Indicators
Many applications display:
Loading spinners
Progress bars
Processing messages
Waiting for these elements to disappear often provides better synchronization than waiting for arbitrary time delays.
Best Practices
Prefer Explicit Wait for AJAX-based applications.
Wait for loading indicators to disappear whenever possible.
Use appropriate Expected Conditions.
Avoid unnecessary use of
time.sleep().Use reliable locators for dynamic elements.
Choose reasonable timeout values based on application behavior.
Conclusion
AJAX Synchronization is an essential synchronization technique for modern Selenium automation. Since AJAX requests load content asynchronously, Selenium must intelligently wait until the webpage is ready before interacting with its elements. By combining WebDriverWait with appropriate Expected Conditions such as invisibility and visibility checks, automation scripts become faster, more reliable, and better suited for real-world web applications.
Frequently Asked Questions (FAQs)
What is AJAX Synchronization in Selenium?
AJAX Synchronization allows Selenium to wait for dynamically loaded content before interacting with web elements.
Why is AJAX Synchronization important?
Modern web applications frequently load data asynchronously. Proper synchronization prevents premature interactions and reduces test failures.
Which wait is commonly used for AJAX Synchronization?
WebDriverWait combined with Expected Conditions is the most commonly used synchronization mechanism.
Can AJAX Synchronization wait for loading spinners?
Yes.
Selenium can wait for loading indicators to disappear using:
invisibility_of_element_located()
Is AJAX Synchronization used in real-world projects?
Yes.
It is widely used in professional Selenium automation frameworks because most modern applications load content dynamically.
Key Takeaways
AJAX requests load webpage content asynchronously.
Selenium must synchronize with AJAX-based applications before interacting with elements.
WebDriverWaitand Expected Conditions provide reliable AJAX Synchronization.Waiting for loading indicators to disappear is a common synchronization strategy.
Proper AJAX Synchronization improves automation reliability and execution speed.
Understanding AJAX Synchronization is essential for building robust Selenium automation frameworks.
