facebook data | | Scrape facebook profile | Search

This code automates Facebook login using Puppeteer. It first checks if the user is already logged in, and if not, it fills in the username and password, submits the form, and handles potential CAPTCHAs.

Run example

npm run import -- "Log in to facebook"

Log in to facebook

function enterFacebook() {
    console.log('Facebook: Sign in required');
    var credentials = getCredentials('facebook.com');
    return client.click('input[name*="email"]')
        .keys(credentials.username)
        .pause(1000)
        .then(() => console.log('Facebook: Require password'))
        .click('input[name*="pass"]')
        .keys(credentials.password)
        .submitForm('[type="submit"]')
        .pause(2000)
        .isExisting('.cp-challenge-form')
        .then(is => {
            if (is) {
                throw new Error('captcha');
            }
        });
}

function loginFacebook() {
    return client
        .getUrl()
        .then(url => {
            var loggedIn = url.indexOf('facebook') > -1 && url.indexOf('login') === -1;
            return loggedIn
                ? client
                    .isVisible('input[name*="email"]')
                    .then(is => is ? enterFacebook() : client)
                : client.url('https://www.facebook.com/')
                    .isVisible('input[name*="email"]')
                    .then(is => is ? enterFacebook() : client)
        })
}
module.exports = loginFacebook;
loginFacebook;

What the code could have been:

/**
 * Attempts to log in to Facebook using the provided credentials.
 * 
 * @returns {Promise<Client>} The client instance if successful, or throws an error if a CAPTCHA is encountered.
 */
function enterFacebook() {
    console.log('Facebook: Sign in required');
    
    // Get credentials from external function or object
    const credentials = getCredentials('facebook.com');
    
    // Click on the email input field
    return client
       .click('input[name*="email"]')
        // Type in the username
       .keys(credentials.username)
       .pause(1000) // Pause for 1 second to allow the username to be typed in
       .then(() => console.log("Facebook: Require password"))
        // Click on the password input field
       .click('input[name*="pass"]')
        // Type in the password
       .keys(credentials.password)
        // Submit the form
       .submitForm('[type="submit"]')
        // Pause for 2 seconds to allow the form to be submitted
       .pause(2000)
        // Check if a CAPTCHA is present
       .isExisting('.cp-challenge-form')
       .then(is => {
            if (is) {
                throw new Error('captcha');
            }
            // If no CAPTCHA is present, return the client instance
            return client;
        });
}

/**
 * Attempts to log in to Facebook using the provided credentials.
 * 
 * @returns {Promise<Client>} The client instance if successful.
 */
function loginFacebook() {
    try {
        // Get the current URL
        return client
           .getUrl()
           .then(url => {
                // Check if the user is already logged in
                const loggedIn = url.indexOf('facebook') > -1 && url.indexOf('login') === -1;
                // If logged in, check if the email input field is visible
                return loggedIn
                   ? client
                       .isVisible('input[name*="email"]')
                       .then(is => is? enterFacebook() : client)
                    : client.url('https://www.facebook.com/')
                       .isVisible('input[name*="email"]')
                       .then(is => is? enterFacebook() : client);
            });
    } catch (error) {
        // If an error occurs, log it and return null
        console.error(error);
        return null;
    }
}

module.exports = loginFacebook;

This code snippet defines two functions, enterFacebook and loginFacebook, designed to automate the process of logging into a Facebook account using Selenium.

enterFacebook Function:

  1. Log Message: Prints a message to the console indicating that Facebook sign-in is required.
  2. Get Credentials: Retrieves Facebook login credentials (username and password) from a function called getCredentials (not shown in the snippet).
  3. Fill Username:
  4. Pause: Waits for 1 second.
  5. Log Password:

Functionality Breakdown:

  1. enterFacebook() Function:

  2. loginFacebook() Function:

  3. Module Export:

Purpose:

This code automates the process of logging into Facebook using Puppeteer. It handles both scenarios: logging in when not logged in and handling potential CAPTCHAs.

Let me know if you have any other questions.