Buy fast & affordable proxy servers. Get 10 proxies today for free.
Download our Proxy Server Extension
© Webshare Proxy
payment methods
One of the common tasks in web automation is handling logins, which ensures access to user-specific content and functionalities, such as personalized settings, user-specific data, and protected resources. Effective login management allows you to perform tasks like data scraping from user accounts, automating repetitive actions within a logged-in session, and testing web applications in authenticated states.
In this article, we’ll guide you through various examples of logging in with Puppeteer, including logging into popular websites like Wikipedia, Facebook, Twitter, and Gmail using Google single sign-on. We will also dive deeper into managing session cookies that are essential for maintaining login states across sessions.
Let’s explore practical examples of how to login to various websites using Puppeteer. Whether you need to login to Wikipedia, Facebook, Twitter/X, or Gmail with Google single sign-on, these examples will cover the essential techniques to achieve successful logins using Puppeteer.
In this example, we will demonstrate how to automate the login process for Wikipedia using Puppeteer. Wikipedia requires a username and password for login, making it an excellent simple case study for understanding Puppeteer’s login capabilities.
Before we begin, make sure you have Puppeteer installed in your project. If not, you can install it via npm:
Now, let’s create a new Javascript file wikipedia_login.js and require Puppeteer:
Next, we’ll write the script to automate the login process. This script will open Wikipedia’s login page, fill in the username and password fields, and submit the form.
Save the script and run it using Node.js:
In the above code:
Facebook’s login page requires both a username (email or phone number) and a password, making it an ideal scenario to showcase Puppeteer’s capabilities for handling complex login forms. Assuming that Puppeteer is installed, here’s the script to automate the login process for Facebook:
In this code:
Twitter’s login process includes handling a username (or email) and password, along with potential additional steps like handling two-factor authentication (2FA). Below is the script to automate logging into Twitter. This script will open Twitter’s login page, enter the username and password, and submit the form.
We wait for the username input field to be available using await page.waitForSelector('input[name
="session[username_or_email]"]') and fill it using page.type(). Similarly, we wait for the password input field and fill it. The login form is submitted by clicking the login button identified by the data-testid="LoginForm_Login_Button" attribute. Finally, we wait for the navigation to complete with page.waitForNavigation() to ensure the login process is finished.
If your Twitter account has two-factor authentication (2FA) enabled, you’ll need to handle the additional input for the verification code. This typically involves waiting for the 2FA input field to appear and then entering the code manually or programmatically if you have access to it. Here’s an extended version of the script to handle 2FA:
This script waits for the 2FA input field and enters the 2FA code if required. This ensures the login process is complete, even with additional security steps.
Google Single Sign-On (SSO) involves multiple steps, including email input, password input, and potentially additional verification steps like 2-factor authentication. Below is the script to automate logging into Gmail. This script will open Google’s login page, enter the email and password, and handle any additional verification steps if necessary.
We wait for the email input field to become available and then fill it using page.type(). After entering the email, we click the "Next" button #identifierNext. Similarly, we wait for the password input field, fill it, and click the "Next" button #passwordNext. If two-factor authentication is enabled, we wait for the 2FA input field, enter the code, and handle different types of 2FA inputs (phone or text) by clicking the appropriate button #idvPreregisteredPhoneNext, #totpNext. Finally, we wait for the navigation to complete to ensure that we are logged into Gmail.
In this article, we’ve explored various examples of how to log in with Puppeteer, from Wikipedia to Facebook to Twitter and even Gmail with Google single sign-on. We’ve seen how Puppeteer’s built-in methods, such as page.type(), page.click(), and page.waitForNavigation(), can be used to automate the login process and save time and effort.
But that’s not all! We’ve also delved into session cookies and learnt how to store and manage them for future use. By re-using previous cookies, we can continue the same session and avoid the need to log in repeatedly. This can be particularly useful when testing web applications or automating repetitive tasks.
Cookies in Puppeteer: How to Accept, Save, Load or Clear Them