1.

How to handle Ajax calls in Selenium WebDriver?

Answer»

Using Selenium WebDriver, HANDLING of AJAX calls is one of the common issues. Users will not be aware when the AJAX CALL would be completed and the page has been updated. 

AJAX stands for Asynchronous JavaScript and XML and AJAX ALLOWS the web page to retrieve small amounts of data from the server without reloading the entire page. From the client to server AJAX sends HTTP requests and then process the server’s response without reloading the entire page. Wait commands may not work to handle AJAX control in such a scenario and it is just because the actual page is not going to refresh.

For example, When you click on the submit button, the required information may APPEAR on the web page without refreshing the browser or sometimes it may load in a second or sometimes it may take longer. And you do not have any control overloading time. The best way to handle this kind of situation in selenium is to use dynamic waits that are WebDriverWait in combination with ExpectedCondition.

Some of the methods available are as follows:

  1. titleIs() – titleIs() waits for a page to load with a specific title. wait.until(ExpectedConditions.titleIs(“Google”));
  2. elementToBeClickable() – elementToBeClickable() waits for an element to be clickable i.e. element should be present/displayed/visible on the screen as well as enabled.
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
  3. alertIsPresent() – alertIsPresent () waits for an alert box to appear.
    wait.until(ExpectedConditions.alertIsPresent()) !=null);
  4. textToBePresentInElement() – textToBePresentInElement() waits for an element having a certain string pattern.
    wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be found”));


Discussion

No Comment Found