How do I handle basic authentication with Puppeteer?
Antek N
antek n profile pic

Handling basic authentication with Puppeteer involves providing the username and password in the URL when navigating to a page that requires authentication. Here's a detailed explanation of how to handle basic authentication using 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 basic authentication: To handle basic authentication, you can provide the username and password in the URL when navigating to a page that requires authentication. Use the formatusername:password@domain.com.

1
2
3
4

   const url = 'https://username:password@domain.com';
   await page.goto(url);
   

In this example, replaceusername andpassword with the appropriate credentials anddomain.com with the actual domain that requires authentication. Thegoto() method is used to navigate to the specified URL, which includes the username and password in the format mentioned above. 3. Handling multiple authentication prompts: If a page has multiple authentication prompts or requires authentication at different stages, you can handle each prompt independently using the'dialog' event.

1
2
3
4
5

   page.on('dialog', async (dialog) => {
     await dialog.accept('username:password'); // Provide the credentials for the specific prompt
   });
   

In this code snippet, the'dialog' event is registered on thepage instance. When a dialog appears, the event listener is triggered, and theaccept() method is called on the dialog with the appropriate credentials to handle the authentication prompt. 4. Handling invalid credentials or authentication failures: In cases where the provided credentials are invalid or authentication fails, you can handle the error by listening to the'response' event and checking the status code.

1
2
3
4
5
6
7

   page.on('response', async (response) => {
     if (response.status() === 401) {
       console.log('Authentication failed');
     }
   });
   

This code registers a'response' event listener on thepage instance. When a response is received, the listener checks the status code usingresponse.status(). If the status code is401, it indicates an authentication failure, and you can handle it accordingly. By following these steps, you can handle basic authentication with Puppeteer. By providing the username and password in the URL when navigating to a page, you can bypass basic authentication requirements. Additionally, you can handle multiple authentication prompts or handle authentication failures by listening to the appropriate events and checking the status codes of the responses.