What is the Puppeteer API method to close a browser tab?
Richard W
richard w profile pic

Handling AJAX requests in Puppeteer involves using thepage.waitForResponse() orpage.waitForRequest() methods to wait for specific network requests to complete. Here's a detailed explanation of how to handle AJAX requests in Puppeteer: 1. Launching a new browser instance and creating a new page:

1
2
3
4
5
6
7
8
9
10
11
12
13

   const puppeteer = require('puppeteer');

   (async () => {
     const browser = await puppeteer.launch();
     const page = await browser.newPage();

     // Perform actions with the page here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Handling AJAX requests withpage.waitForResponse(): Thepage.waitForResponse() method allows you to wait for a specific network response based on various criteria such as URL, status code, or response headers.

1
2
3
4
5

   const response = await page.waitForResponse((response) => {
     return response.url().includes('your-ajax-url');
   });
   

In this example,page.waitForResponse() is used to wait for a network response that matches the specified condition. The condition is defined using a callback function that checks if the response URL includes'your-ajax-url'. The method returns a response object that you can use for further processing or validation. 3. Handling AJAX requests withpage.waitForRequest(): If you want to wait for a specific network request to be initiated, you can use thepage.waitForRequest() method. Similar topage.waitForResponse(), you can define a condition to match the request based on URL, method, headers, etc.

1
2
3
4
5

   const request = await page.waitForRequest((request) => {
     return request.url().includes('your-ajax-url');
   });
   

In this code snippet,page.waitForRequest() is used to wait for a network request that matches the specified condition. The condition is defined using a callback function that checks if the request URL includes'your-ajax-url'. The method returns a request object that you can use for further processing or validation. 4. Handling multiple AJAX requests: If you want to handle multiple AJAX requests, you can use a combination ofpage.waitForResponse() orpage.waitForRequest() inside loops or by chaining promises.

1
2
3
4
5
6
7

   const ajaxUrls = ['url1', 'url2', 'url3'];

   const responses = await Promise.all(ajaxUrls.map((url) => {
     return page.waitForResponse((response) => response.url().includes(url));
   }));
   

In this example, an array of AJAX URLs is defined.Promise.all() is used to wait for multiple responses by mapping each URL to apage.waitForResponse() call. The method returns an array of response objects that you can use for further processing or validation. By following these steps, you can handle AJAX requests in Puppeteer. By usingpage.waitForResponse() orpage.waitForRequest(), you can wait for specific network responses or requests that match specified conditions. This allows you to synchronize your automation with AJAX calls and perform actions or validations based on the received data.