linkedin messages | | Scrape LinkedIn profile | Search

This code provides a function loginLinkedIn that automates the process of logging into LinkedIn using Selenium, handling potential CAPTCHA challenges and returning a boolean indicating success or failure.

Run example

npm run import -- "Log in to LinkedIn"

Log in to LinkedIn

var notRobot = () => {
    return client.click('')
}

function enterLinkedIn() {
    console.log('LinkedIn: Sign in required');
    var credentials = getCredentials('linkedin.com');
    return client.isExisting('a[href*="uas/login"]')
        .then(is => is ? client.click('a[href*="uas/login"]') : Promise.resolve([]))
        .then(() => client.click('input[name*="session_key"]'))
        .keys(credentials.username)
        .pause(1000)
        .then(() => console.log('LinkedIn: Require password'))
        .click('input[name*="session_password"]')
        .keys(credentials.password)
        .submitForm('.login-form, [type="submit"]')
        .pause(2000)
        .isExisting('.cp-challenge-form')
        .then(is => {
            if (is) {
                throw new Error('captcha');
            }
        });
}

function loginLinkedIn() {
    return client
        .alertText()
        .then(t => t.indexOf('leave') > -1 ? client.alertAccept() : '')
        .catch(e => {
        })
        .pause(1000)
        .getUrl()
        .then(url => {
            var loggedIn = url.indexOf('linkedin') > -1 && url.indexOf('login') == -1
                 && url.indexOf('authwall') == -1;
            console.log(loggedIn);
            return loggedIn
                ? client
                    .isExisting('iframe.authentication-iframe').then(is => is
                        ? client.element('iframe.authentication-iframe')
                            .then(el => client.frame(el.value))
                            .then(() => enterLinkedIn())
                            .frame()
                        : Promise.resolve([]))
                : client.url('https://www.linkedin.com/')
                    .isExisting('*=Forgot password?').then(is => is
                        ? enterLinkedIn()
                        : Promise.resolve([]));
        })
};
module.exports = loginLinkedIn;

What the code could have been:

const { Client } = require('nightwatch-api');

const loginLinkedIn = async (client) => {
    // Check if not robot
    await client.checkNotRobot();

    // Attempt to log in
    try {
        // Check for sign-in required
        const signinRequired = await client.isExisting('a[href*="uas/login"]');
        if (signinRequired) {
            // Click sign-in button
            await client.click('a[href*="uas/login"]');
        }

        // Log in
        await loginWorkflow(client);

        // Check if logged in
        const loggedIn = await client.getUrl().then(url => {
            return (url.indexOf('linkedin') > -1 && url.indexOf('login') == -1
                && url.indexOf('authwall') == -1);
        });

        // Log in successfully
        if (loggedIn) {
            // Switch to frame
            await client.switchToFrame('authentication-iframe');
            await enterLinkedIn(client);
        } else {
            // Log out or refresh page
            console.log('Logged out or refresh page');
            await client.url('https://www.linkedin.com/');
        }
    } catch (error) {
        // Handle errors
        if (error.message === 'captcha') {
            console.error('Captcha detected');
        } else {
            console.error('Unknown error', error);
        }
        return Promise.reject(error);
    }
};

const enterLinkedIn = async (client) => {
    // Get credentials
    const credentials = await getCredentials('linkedin.com');

    // Click login button
    await client.click('a[href*="login"]');

    // Fill in username
    await client.click('input[name*="session_key"]');
    await client.keys(credentials.username);

    // Wait for password prompt
    await client.pause(1000);

    // Click password button
    await client.click('input[name*="session_password"]');

    // Fill in password
    await client.keys(credentials.password);

    // Submit form
    await client.submitForm('.login-form, [type="submit"]');
    await client.pause(2000);

    // Check for captcha
    const captcha = await client.isExisting('.cp-challenge-form');
    if (captcha) {
        throw new Error('captcha');
    }
};

const loginWorkflow = async (client) => {
    // Check for alert
    const alert = await client.alertText();
    if (alert && alert.indexOf('leave') > -1) {
        await client.alertAccept();
    }

    return client;
};

const getCredentials = async (url) => {
    // Get credentials from storage or db
    // TODO: implement credentials storage
    throw new Error('Credentials not implemented');
};

module.exports = loginLinkedIn;

This code defines a function loginLinkedIn that automates the process of logging into LinkedIn using Selenium.

Here's a breakdown:

  1. notRobot Function:

  2. enterLinkedIn Function:

  3. loginLinkedIn Function:

  4. Module Export:

Let me know if you have any more questions!