How to Send Connect Request in LinkedIn using Selenium VBA?
LinkedIn is one of the most influential social media networks. LinkedIn enables you to network with similar industry people, and professional organizations in your industry as well as others. You can invite anyone to connect and accept their invitations to connect with you. If you are a company/professional and want to connect a large number of profiles then manual sending LinkedIn Connect requests is a time-consuming task. In this blog, we will learn how we can create a Selenium script in VBA to send automatic Connect requests by already saved LinkedIn profile URL in MS Excel. First, you need to install Selenium for VBA.
Step 1: Create MS Excel Macro-Enable file
- Create a new MS Excel Macro-Enable file with the name LinkedInProfile.xlsm.
- Add LinkedIn profile URL in Column A as shown in the figure.
- Add two buttons from Developer->Insert->Button with the name Open Browser, Send Connect.
Step 2: Setup VBA Editor for script
- Open the VBA editor by pressing Alt+F11 or 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 choose Selenium Type Library.
Step 3: Create 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. Then next time we don’t need to log in again.
- Add the below code in 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 in 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 form 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 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 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 4: Assign Macros to Buttons
- Close VBA Editor if it is already open.
- Right-click on Open Browser and assign macro OpenBrowser. As well as assign ConnectProfile to Send Connect button.
Step 5: Running Script
- Click on the Open Browser button, it will open the Chrome browser. Open LinkedIn and log in with your credentials.
- In Excel paste all LinkedIn profiles in the B column to send Connect requests and Save data.
- Now click on Send Connect button. It will read the LinkedIn profiles from the B column one by one and click on Connect button to send requests for all profiles.
- After all, profiles are complete, the script will show the message Script is completed successfully!
Last Updated :
05 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...