Let’s see, How can you handle Ajax in Selenium. In these days, many websites are using AJAX technology. In AJAX, the web elements get refresh without refreshing the Web Page. In AJAX driven web applications, data is retrieved from the server without refreshing the page.
When we perform any action on Ajax controls, Wait commands will not work as the page is not actually refreshed here.
Pausing the test execution using threads for a certain period of time is also not a good approach as web element might appear later or earlier than the mentioned period of time.
The best approach to handle the AJAX calls would be to wait for the required element in a dynamic period and then continue the test execution as soon as the element is found/visible.
And we know This can be achieved with WebDriverWait in combination with ExpectedCondition (i.e explicit wait).
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
Handle Ajax in Selenium
There are many methods which are available to use with wait.until(ExpectedConditions.anyCondition); The below is the image for the number of methods which are available.
1. elementToBeClickable() – The expected condition waits for an element to be clickable i.e. it should be present/displayed/visible on the screen as well as enabled.
2. textToBePresentInElement() – The expected condition waits for an element having a certain string pattern.
wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be found”));
3. alertIsPresent() – The expected condition waits for an alert box to appear.
4. titleIs() – The expected condition waits for a page with a specific title.
wait.until(ExpectedConditions.titleIs(“Deal of the Day”));
5. frameToBeAvailableAndSwitchToIt() – The expected condition waits for a frame to be available and then as soon as the frame is available, the control switches to it automatically.