Automating Linked-In Connections with Python

Rohan Chavan
5 min readDec 11, 2018

Hii guyzz, this is an writeup of how I automated LinkedIn with python to send connections request automatically to all the suggested users.Below is the working video of the code.Modules used :- selenium, pyautogui.

What is Linked-In ?

(As said by Lifewire)LinkedIn is a social network for professionals. Whether you’re a marketing executive at a major company, a business owner who runs a small local shop or even a first year college student looking to explore future career options, LinkedIn is for anybody and everybody who’s interested in taking their professional life more seriously by looking for new opportunities to grow their careers and to connect with other professionals.

The need for automation ?

I am currently a studying computer Engg , but side by side I am a security researcher, bug bounty hunter, CTF guy and want to work as a Penetration tester in the future.I heard that there is a nice infosec community on linked in and having a profile there would help me get a job easily in future. So I created my account and was manually sending connection requests to people, after some time I saw that linked in suggests connections with the same interest or bio, just like facebook.The logic is simple, the more connections you have, big is your network,Now I was simply clicking the connection button again and again without actually seeing the profile of the person, then I thought what if I let python do what I was doing manually (Benefits of being lazy).

Let the code speak now,

For most of the web automation, you (mostly) need two things a scrapping package like beautifulsoup and requests, Now if there isn’t much scrapping involved you could also use regex.Selenium is a package which is used to automate browsers, you can read more about it here. Along with selenium we need a webdriver, you need to download the webdriver according to your browser and its version, you can simply say that webdriver is an interface between your code and the browser. I have also used PYautoGUI , it is a python module for programatically controlling keyboard and mouse.

let start with Importing all the things we need,

from selenium import webdriver #connect python with webbrowser-chrome
from selenium.webdriver.common.keys import Keys
import pyautogui as pag
import time

Now lets add the main function,

First we need to open the linked in page in our automated browser, lets do it.We will write it in our main function,

def main():
#launch url
url = “http://linkedin.com/"
nurl = “http://linkedin.com/mynetwork/"
driver = webdriver.Chrome('C:\\Users\\hp\\Desktop\\chromedriver.exe')
driver.get(url)
if __name__ == __main__:
main()

Here the variable url is simply the url of linkedin, (Ignore the nurl var for now). Then we call our webdriver using webdriver.chrome(/path-to-your-driver/) since it might be big and a pain to write everytime, we define a variable driver for it.

Now that we have opened the login page, we obviously need to signin,

lets write a function for that,

# Login to Linked in :
def login():
username = driver.find_element_by_id(“login-email”)
username.send_keys(“username”)
password = driver.find_element_by_id(“login-password”)
password.send_keys(“password”)
driver.find_element_by_id(“login-submit”).click()
print(“successfully logged in”)

find_element_by_id :- Using selenium you can find html contents by id, name, tag, class, etc. so here firstly we need to find the element in login page with the id = “login-email” . How did i get the element id ? simple just go to the desired element and inspect element, there you will see all the attributes of an html entity.

Once selenium gets that tag, now we need to send the keys, that’s why we imported keys, it can be simply done with the send_keys function in selenium.we use the same technique to enter the password, once entered the creds we need to click the login button , well selenium got it covered.It has an inbuilt click function which does it for you.

Now we need to goto the networks tab in Linked-In

Using the same logic above we can write it as,

def goto_network():
try:
driver.find_element_by_id(“mynetwork-tab-icon”).click()
except:
print(“[+] Some Error Occoured \n Directly opening networks tab”)
body = driver.find_element_by_tag_name(“body”)
body.send_keys(Keys.CONTROL + ‘t’)
driver.get(nurl)

We simply start with a try except block, if selenium isn’t able to open it, the code will open the nurl (see the variable defined above) page , this isnt necessary but it will serve as a backup plan.

Sending the Connection requests :-

Big platforms always try their best to prevent scrapping and automation, and for this they use various techniques, same was done by linked-in . Since the connections was in a dymanic table , I wasnt able to point selenium to that connection button, I had tried Xpath and all the ways possible, but it wasnt possible. After spending a day on it, I got another idea, I saw that the connection button is in the same area, (Use linkedin and you will understand what i am talking about). So I used another approach, I took screenshot of the page and opened it in paint, then I calculated where the connect button lies, It was on (880,700)px ( It will be different for you). So now you need to click on pixel 880,700 to click on this connection button , this can be written as below

def get_details():
test = input(“ start sending requests (y/n)”)
for i in range(0,10):
pag.click(880,700)
time.sleep(2)
print(“[“+str(i)+”] Connection request sent “)
print(“done”)

Here the input is simply used as a user controlled pause in the program flow,The for loop should iterate the number of times, you want to send the connection requests.I have sent it 10 times(quite enough for a demo).

To click on the pixel, we used pyautogui so,

pag.click(880,700)

clicks on the mentioned pixel,Thats it , your script just sent someone a connection request ..!!!

The Complete code :-

I have purposely not shared the github link, but if you want the code, just comment your email Id, I will share the github link.

--

--

Rohan Chavan

Python Full Stack Dev | GO | Security Enthusiast | Bug Bounty | Automation