Python Selenium – LinkedIn connect and message bot

In this project we will use Selenium and a Chrome web driver to create a script that will connect with and message people on LinkedIn. Prerequisite of the project is that you have a LinkedIn account and a Chrome web driver stored on your machine. You may download the web driver by clicking here, note that it needs to match your current version of Chrome.

We begin by logging in to LinkedIn, this has been covered in a separate post found here. The snippet of code below will open a browser window and log us in to the LinkedIn account. The whole script is one big LinkedIn session, I’ve found that by logging out and back in constantly will cause issues i.e. being forced to confirm your not a robot. To overcome this I remain logged in via this session and apply appropriate delays between each connection request to come across as human as possible – it works OK so far…

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import openpyxl
from random import *
import time

#Specifies the location of the web driver
driver = webdriver.Chrome("C:\Chromium\chromedriver.exe")
#The driver.get() method will navigate to the given URL Address
#Applying a delay for the web page to load

#Locating the email field on the Linkedin login page
username = driver.find_element_by_xpath('//*[@type="text"]')
#Entering in an email address 
#Locating the password field on the Linkedin login page
password = driver.find_element_by_xpath('//*[@type="password"]')
#Entering in a password
#Locating the Login button on the Linkedin login page
log_in_button= driver.find_element_by_xpath('//*[@class="sign-in-form__submit-button"]')
#Clicking on the Login button
#Applying a delay for the web page to load

In this example we have an Excel spreadsheet populated with LinkedIn profile URL’s and first names, we’ll be using the Openpyxl library to iterate through this data – more on this covered here.

#Loading our Excel spreadsheet data
wb = openpyxl.load_workbook('Spreadsheet.xlsx')
#Defining the name of the Sheet within the workbook
sheet = wb["Sheet"]
max_col = sheet.max_row

#For loop to iterate the data within the spreadsheet 
for i in range(1,sheet.max_row+1):
    #Column 1 set to be the Firstname
    FirstName = str(sheet.cell(row = i, column = 1).value)
    #Column 2 set to be the LinkedIn URL
    LinkedInURL = str(sheet.cell(row = i, column = 2).value)

For each row of data iterated we then redirect the browser to the LinkedIn URL, click on the connect button and add a note to the invitation. In the invitation we’ll be addressing them by their first name as seen within the spreadsheet data. The below code is a continuation of the for loop above.

#The driver.get() method will navigate to the given URL Address    
#Applying a delay for the web page to load

#Below looks for the Connect button on a users profile via Xpath and then clicks the button.
try:      driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div/div[2]/div/div/main/section[1]/div[2]/div[3]/div/button/span').click()
except:   driver.find_element_by_xpath('/html/body/div[6]/div[3]/div/div/div[2]/div/div/main/section[1]/div[2]/div[3]/div/button').click()

#Finding and clicking the 'Add note' option

#Locating the text Area
TextField = driver.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/div/textarea')

#Sending keys for our custom note
TextField.send_keys('Hi '+FirstName+',')
TextField.send_keys('Hope your having a wonderful day.')

#Defining a random value for our delay (15-20 minutes)
sleepInt = randint(900, 1200)

Once all spreadsheet data has been iterated and all connection requests have been sent add the following code outside of the for loop to close your web driver.


Leave a Reply