Handle Ajax Calls using Selenium:
Handling AJAX calls is one of the common issues when using Selenium WebDriver. We wouldn’t know when the AJAX call would get completed and the page has been updated. In this post, we see how to handle AJAX calls using Selenium.
When you click on a submit button, required information may appear on the web page without refreshing the browser. Sometimes it may load in a second and sometimes it may take longer. We have no control on loading time. The best approach to handle this kind of situations in selenium is to use dynamic waits (i.e. WebDriverWait in combination with ExpectedCondition)
Some of the methods which are available are as follows:
Check this post for detailed explanation on WebDeriverWait
In the above post, we have discussed on titleIs() condition. Let’s see some other conditions in detail.
- titleIs() – The expected condition waits for a page with a specific title.1wait.until(ExpectedConditions.titleIs(“Deal of the Day”));
- 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.1wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
- alertIsPresent() – The expected condition waits for an alert box to appear.1wait.until(ExpectedConditions.alertIsPresent()) !=null);
- textToBePresentInElement() – The expected condition waits for an element having a certain string pattern.1wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be found”));
If you are not a regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below link.