
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Control Flow
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - Emitting Code
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium - Double Click
- Selenium - Right Click
- HTML Report in Python
- Handling Edit Boxes
- Selenium - Single Elements
- Selenium - Multiple Elements
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Pen Events
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - ThreadGuard
- Selenium - Errors & Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium - DevTools
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github Tutorial
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - IE Driver
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
Selenium WebDriver - Handling Ajax Calls
Selenium Webdriver can be used to handle Ajax calls. Ajax also known as Asynchronous JavaScript is vastly made up of XML and JavaScript. From the UI point of view, a JavaScript call is made to the server and we receive the response in XML format from the server.
What is an Ajax?
Ajax or Asynchronous JavaScript is used mostly to create quick and responsive web pages. Let us take an example of a web page, where the user information updates on click of a button. Suppose a user is updating his information quite frequently, because of that, the whole web page needs to be reloaded each time. However, on a web page which is built on Ajax, only the part of user information which the user modified should be updated, rather than reloading the whole page.
Ajax is compliant with technologies like the HTTP request, JavaScript, XML, HTML, CSS, and so on. Ajax directs and receives data asynchronously without page reloading.
In an Ajax system, the user interface sends a JavaScript call to the XML HTTP request callback method, then a HTTP request is directed to the web server. Next, the web server on the basis of request exchanges information with the database, and fetches it in the JSON/XML format to reflect in the user interface.
How does Selenium Handle an Ajax Call?
Selenium Webdriver is not always successful in accessing the web elements post an Ajax call. This is because in an Ajax web application, the wait time for an element to be available is not uniform. Selenium tests wait for a specific amount of time after which we encounter failures. Inability to predict an Ajax call time is a challenge for the Selenium tests.
In order to overcome this, Selenium uses the the below synchronization and wait mechanisms −
Thread.sleep()
This command pauses the execution for the amount of time passed as a parameter. However, this is not a good choice, since the wait time is fixed, however, the Ajax call time cannot be predicted. Also, this command moves the current thread from the running queue to waiting.
Implicit Wait
It is the default wait available in Selenium. It is a kind of global wait applicable to the whole driver session. The default wait time is 0, meaning if an element is not found, an error will be thrown straight away. However, if a wait time is set, an error will be thrown once the wait time surpasses. Once the element is identified, its reference is returned and execution moves to the next step. We should use implicit wait in an optimal way, a larger wait time would increase the execution time of the tests.
Explicit Wait
This type of wait is ideal for handling the Ajax calls. Some of the expected conditions for explicit waits are listed below −
- titleContains
- alertIsPresent
- invisibilityOfElementLocated
- titleIs
- invisibilityOfElementWithText
- visibilityOf
- textToBePresentInElement
- visibilityOfElementLocated
- visibilityOfAllElements
- presenceOfAllElementsLocatedBy
- presenceOfElementLocated
- elementToBeClickable
- stalenessOf
- textToBePresentInElementValue
- textToBePresentInElementLocated
- elementSelectionStateToBe
- elementToBeSelected
- frameToBeAvaliableAndSwitchToIt
Fluent Wait
This is the maximum time the driver waits for a specific condition for an element to be true. It also determines the interval at which the driver will verify(polling interval) prior to locating an element or throwing an exception.
Handling Ajax with Explicit Wait
Let us take an example of the below page, where we would click on Click Me.

On clicking on Click Me, we would take the help of the explicit wait and hold back for the appearance of the text You have done a dynamic click on the web page.

Example
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.concurrent.TimeUnit; public class ExplicitsWait { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser driver.get("https://www.tutorialspoint.com/selenium/practice/buttons.php"); // identify button then click on it WebElement l = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[1]")); l.click(); // Identify text WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); // explicit wait to expected condition for presence of a text WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2)); wt.until(ExpectedConditions.presenceOfElementLocated (By.xpath("//*[@id='welcomeDiv']"))); // get text System.out.println("Get text after clicking: " + e.getText()); // Quitting browser driver.quit(); } }
Output
Get text after clicking: You have done a dynamic click Process finished with exit code 0
In the above example, the text that appeared after clicking on the Click Me button was You have done a dynamic click.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Handling Ajax with Fluent Wait
Let us take another example of the below page where we would perform click on Color Change.

On performing a click on Color Change, we would take the help of the fluent wait and hold back for the appearance of the button with the text Visible After 5 Seconds on the web page.

Example
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import java.time.Duration; public class Fluentwaits { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/dynamic-prop.php"); // identify button then click WebElement l = driver.findElement(By.xpath("//*[@id='colorChange']")); l.click(); // fluent wait of 6 secs till other button appears Wait<WebDriver> w = new FluentWait<WebDriver>(driver) .withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofSeconds(6)) .ignoring(NoSuchElementException.class); WebElement m = w.until (ExpectedConditions.visibilityOfElementLocated (By.xpath("//*[@id='visibleAfter']"))); // checking button presence System.out.println("Button appeared: " + m.isDisplayed()); // Quitting browser driver.quit(); } }
Output
Button appeared: true
Handling Ajax with Implicit Wait
Let us take an example of the below page, where we would make an attempt to locate the text Selenium Automation Practice Form with an incorrect xpath value and use an implicit wait. During that time, once the timeout time has passed, NoSuchElementException should be thrown.
The correct xpath for this element would be: /html/body/div/header/div[2]/h1. But, we would use erroneous an incorrect xpath - /html/body/div/header/div[2]/u1 in our implementation in order to generate an exception.

Example
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class Implicitwaits { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php"); // identify element with incorrect xpath value WebElement l = driver.findElement(By.xpath("/html/body/div/header/div[2]/u1")); l.click(); // get text System.out.println("Get text : " + l.getText()); // Quitting browser driver.quit(); } }
Output
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/header/div[2]/u1"} (Session info: edge=121.0.6167.160) For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Process finished with exit code 1
In the above example, we had obtained the NoSuchElementException since an erroneous xpath value was used to locate the text. Once the implicit wait time of 2 seconds passed, we got an exception.
Finally, the message Process finished with exit code 1 is received, signifying unsuccessful execution of the code.
Conclusion
This concludes our comprehensive take on the tutorial on Selenium Webdriver Handling Ajax Calls. Weve started with describing what an Ajax is, how Selenium handles Ajax, and walked through examples of how to use explicit, implicit, and fluent waits to handle Ajax along with Selenium. This equips you with in-depth knowledge of the handling Ajax Calls. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.