How to Send Connect Request in LinkedIn using Selenium VBA

How to Send Connect Request in LinkedIn using Selenium VBA

Do you want to build your LinkedIn Connection by sending connect requests, but don’t know how to send a large number of requests?

If you are a company or professional looking to connect with a large number of profiles, manually sending LinkedIn connection requests can be a time-consuming task.

In this tutorial, we will guide you to build a Selenium script in Excel VBA to automate the process of sending connection requests by using saved LinkedIn profile URLs stored in an Excel spreadsheet.

Before you get started, be sure to install Selenium for VBA by following the How to Install Selenium Webdriver for VBA in Windows tutorial. If you have successfully installed Selenium for VBA, then follow these step-by-step instructions to build the script to send connect requests on LinkedIn.

Table of Contents

  1. Create MS Excel Macro-Enable file
  2. Setup VBA Editor for script
  3. Create an Open Browser Script
  4. Create Send Connect Request Script
  5. Assign Macros to Buttons
  6. Running Script

Step 1: Create MS Excel Macro-Enable file

  • Create a new MS Excel Macro-Enable file with the name LinkedInProfile.xlsm.
  • Add LinkedIn profile URLs in Column A as shown in the figure.
  • Add two buttons from the menu Developer->Insert->Button with the name Open Browser, Send Connect as shown in the image.

Step 2: Setup VBA Editor for script

  • Open the VBA editor by pressing Alt+F11 or from the menu Developer->Visual Basic.
  • In VBA Editor, insert a new module from the menu Insert->Module. New module Module1 will be added to your VBA Project.
  • Add reference for Selenium by menu Tools->References and check Selenium Type Library as shown below image.

Step 3: Create an Open Browser Script

  • First, we need to create a script that will open the browser. But why do we need a separate button and code for opening the browser? This is because we don’t want to log in to LinkedIn each time when the browser loads. So the first time when we open the browser then we will log in with our LinkedIn credentials. The next time we don’t need to log in again.
  • Add the below code to the newly created Module1.
  • Note: The code driver.AddArgument “user-data-dir=” & ActiveWorkbook.Path & “\vbauserdata” means that we are defining the user data directory so all our logins/history/cache will be saved in this vbauserdata directory. This directory will be created in the same directory where our LinkedInProfile.xlsm is saved.
Option Explicit

' Global Variables
Dim driver As New WebDriver
Dim By As New Selenium.By   

Sub OpenBrowser()
    On Error GoTo ErrorHandler
    
    If MsgBox("Are you sure to open Chrome Browser?", vbQuestion + vbYesNo) = vbNo Then Exit Sub    
    
    driver.AddArgument "user-data-dir=" & ActiveWorkbook.Path & "\vbauserdata"
    driver.Start "Chrome"
    driver.Get "https://www.linkedin.com/"    
    
    Exit Sub
ErrorHandler:
    MsgBox Err.Description, vbCritical
End Sub

Step 4: Create Send Connect Request Script

  • Add the below code to the newly created Module1 just after the previous code.
Sub ConnectProfile()
    On Error GoTo ErrorHandler

    Dim i As Integer, startRow As Integer, endRow As Integer
    Dim linkedInURL As String
    
    '''Our profile will start from row 2 and can read till 5000 rows or no profile found
    startRow = 2
    endRow = 5000

    Application.DisplayStatusBar = True

    ''' Read Excel file
    For i = startRow To endRow
        linkedInURL = ""
        
        linkedInURL = Sheets("ProfileURL").Range("B" & i).Value

        ''If there is no LinkedIn URL then exit the script
        If InStr(linkedInURL, "linkedin") = 0 Then
            Exit For
        End If

        Application.StatusBar = "Running: " & linkedInURL

        ' Open current profile
        driver.Get linkedInURL
        
        ' HTML Code is taken by Inspecting the element
        If driver.IsElementPresent(By.XPath("//div[@class='pvs-profile-actions ']//button[contains(@aria-label, 'Invite ')]")) = True Then
            Set connectButton = driver.FindElementByXPath("//div[@class='pvs-profile-actions ']//button[contains(@aria-label, 'Invite ')]")
            
            driver.ExecuteScript "arguments[0].click();", connectButton
            
            '' Send Connect Request
            Set sendButton = driver.FindElementByXPath("//button[@aria-label='Send now']")
            driver.ExecuteScript "arguments[0].click();", sendButton
    
            Application.Wait Now + #12:00:02 AM#
            
            Sheets("ProfileURL").Range("C" & i).Value = "Connect Request Sent"
        End If
    Next i

    Application.StatusBar = ""
    MsgBox "Script is completed successfully!", vbInformation

    Exit Sub
ErrorHandler:
    MsgBox Err.Description, vbCritical    
    Application.StatusBar = ""
End Sub

Step 5: Assign Macros to Buttons

  • Close VBA Editor if it is already open.
  • Right-click on the Open Browser button and assign macro OpenBrowser. As well as assign ConnectProfile macro to Send Connect button.

Step 6: Running Script

  • Click on the Open Browser button, it will open the Chrome browser. As well as it will open the LinkedIn website. Use your credentials to log in to LinkedIn.
  • In Excel paste all LinkedIn profiles in column B to send Connect requests and Save data.
  • Now click on Send Connect button. It will read the LinkedIn profiles from column B one by one and click on the Connect button to send requests for all profiles.
  • When all profiles are complete, the script will show the message Script is completed successfully!

Related: See our guide on How to Send Messages on LinkedIn using Selenium VBA for more details.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top