How To Handle AJAX Calls Using Selenium WebDriver
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.
AJAX stands for Asynchronous JavaScript and XML. AJAX allows the web page to retrieve small amounts of data from the server without reloading the entire page. AJAX sends HTTP requests from the client to server and then process the server’s response without reloading the entire page. To handle AJAX controls, wait commands may not work. It’s just because the actual page is not going to refresh.
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.
wait.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.
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
- alertIsPresent() – The expected condition waits for an alert box to appear.
wait.until(ExpectedConditions.alertIsPresent()) !=null);
- textToBePresentInElement() – The expected condition waits for an element having a certain string pattern.
wait.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.
How to click on a element which is not visible on the screen?
Hi Saili Inamdar,
Its not possible to click on an element which is not visible on the screen. You have to wait for the element to be loaded. Check out this post
Thanks
Thanks Raj for your reply.
Now in my case, the web URL, doesn’t have scroll bar so that element will not be visible unless you zoom in the page. I do not want to use zoom in zoom out functionality as it is not recommended in my project. Then in this scenario, what should i do? Please guide.
Hi Saili Inamdar,
Can you please let me know
1. Why it’s not recommended to use zoom in and zoom out.
2. How you achieve the same behavior manually.
Hi Raj,
Zoom in zoom out is not recommended as higher authorities don’t want to change zoom from 100%.
Secondly, guys those who are doing manual testing they perform zoom in zoom out. So BA out here is not allowing to have scroll bar and so i am unable to proceed.
Hi Saili Inamdar,
Selenium is a tool which automates browser. Selenium wont interact with hidden elements. But try JavaScriptExecutor. As you said even your manual testers are doing the same process. If nothing works, try to convince your team to run the script using zoom in zoom out. Also let me know the outcome of yours. Thanks.
Hi Raj,
i am waiting for your comment and solution as well. Please guide me on the same.
Thanks Raj. I will try for JavascriptExecutor and let you know.
I tried doing by zoom in zoom out for submenu element but it is not working in my case. Can you please guide me on this as well so that any one solution would work.