LinkedIn Auto Connect Bot with Personalized Messaging

Published by georgiannacambel on

Auto Connect Bot for LinkedIn

In this project, we are going to create a bot that finds the people in your LinkedIn suggestions and sends a connection request to each one of them with a message. It also finds the suggestions of your suggestions and sends them a connection request.

You will need to install selenium and beautifulsoup4. Below are the commands which you need to run to download both the packages. You can also visit the given links for more information about installation.

For Selenium:-

!pip install selenium

For beautifulsoup4:-

!pip install beautifulsoup4

Besides these packages you also need to download a web driver for your browser. Based on your Google Chrome version you can download the driver from here. Save it in the working repository.

Lastly, in the config.txt file you need to add your email id and LinkedIn password and save that file in the working repository.


Here we have imported the necessary libraries.

import os, random, sys, time
from selenium import webdriver
from bs4 import BeautifulSoup

Here we are getting the address of the Google Chrome driver. After you run this line a new Google Chrome window will open. As we have not passed any link we will get a blank window.

browser = webdriver.Chrome('driver/chromedriver.exe')

Now we will open the LinkedIn login page using browser.get().


We will open the config.txt file which we have created and read the username and password from the file.

file = open('config.txt')
lines = file.readlines()
username = lines[0]
password = lines[1]

Now we have to automate the login process. For that, we will have to check the id of the textboxes which accept the username and password on the webpage. We can do this by right-clicking anywhere on the webpage and then clicking on 'inspect'. After doing this you will see that the id of the username textbox is username and the id of password textbox is password.

find_element_by_id() returns the first element with the id attribute value matching the location.  The send_keys() method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc. It replaces its contents on the webpage in your browser. submit() method is used to submit a form after you have sent data to a form.

Once you pass the username and password you can see them on the webpage before submitting.

Note:- The IDs of the textboxes can change. Hence before running this code check the current ID of the textboxes by inspecting the webpage.

elementID = browser.find_element_by_id('username')

elementID = browser.find_element_by_id('password')


Now we need to create links to visit different profiles. remains same irrespective of whose profile we visit. The profile ID is appended after it. The profile ID is different for different profiles. The code below will open the profile whose profile ID is /in/aarya-tadvalkar-092650193/.

visitingProfileID = '/in/aarya-tadvalkar-092650193/'
fullLink = '' + visitingProfileID

Now we will write a function that returns the list of the profile IDs of the people which are suggested to us by LinkedIn. We will send the entire page source to this function. To get the entire source code we will use BeautifulSoup(browser.page_source). This source code will be available in the soup variable in the function. First, we will find the div with class name pv-browsemap-section. From that, we will find all the a tags with class name pv-browsemap-section__member. The href in these a tags contain the profile IDs. If the profile ID is not already there in the profilesQueued and if it is not visited i.e. it is not there in the visitedProfiles we will append it to profilesID. At the end profilesID will contain the links and it will be returned.

Note:- The class names used here can change. Hence before running this code check the current class name by inspecting the webpage.

visitedProfiles = []
profilesQueued = []

def getNewProfileIDs(soup, profilesQueued):
    profilesID = []
    pav = soup.find('div', {'class': 'pv-browsemap-section'})
    all_links = pav.findAll('a', {'class': 'pv-browsemap-section__member'})
    for link in all_links:
        userID = link.get('href')
        if((userID not in profilesQueued) and (userID not in visitedProfiles)):
    return profilesID

getNewProfileIDs(BeautifulSoup(browser.page_source), profilesQueued)
profilesQueued = getNewProfileIDs(BeautifulSoup(browser.page_source), profilesQueued)

Now for each link in profilesQueued we are going to perform the following actions:-

  • We will append the Profile ID to the base link  which is common to all. Then we will get the fullLink. We will visit the fullLink.
  • Now we have to send a connection request to the person. For that, we will find the class name of the button which says connect. The class name is pv-s-profile-actions. We will find the element using the class name and then click on it using .click().
  • After you click the connect button you will get a prompt asking if you want to add a note. We will click on the button add a note by using its class name which is mr1.
  • Now you will get a text area. We will select the text area using its id custom-message. We will send our message using send_keys().
  • After that, we will have to select the button done which has class ml1 and click it.
  • Then we will add the current ID to the file visitedUsers.txt.
  • Next, we will get the suggested profiles of the current profile using the function getNewProfileIDs(). We will add these new profile IDs to profilesQueued.
  • Then we have just introduced a random delay so that LinkedIn doesn't detect a bot.
while profilesQueued:
        visitingProfileID = profilesQueued.pop()
        fullLink = '' + visitingProfileID



        customMessage = "Hello, I have found mutual interest area and I would be more than happy to connect with you. Kindly, accept my invitation. Thanks!"
        elementID = browser.find_element_by_id('custom-message')


        # Add the ID to the visitedUsersFile
        with open('visitedUsers.txt', 'a') as visitedUsersFile:

        # Get new profiles ID
        soup = BeautifulSoup(browser.page_source)
            profilesQueued.extend(getNewProfileIDs(soup, profilesQueued))

        # Pause
        time.sleep(random.uniform(3, 7)) # Otherwise, sleep to make sure everything loads

            print('Visited Profiles: ', len(visitedProfiles))

            with open('profilesQueued.txt', 'a') as visitedUsersFile:
            print('100,000 Done!!!')


Leave a Reply

Avatar placeholder