Site icon DataFlair

Automate Linkedin Connections with Python

Python course with 57 real-time projects - Learn Python

LinkedIn is a social platform for professional networking where people network with other people as well as exhibit their skills by demonstrating projects and sharing their accomplishments. Connecting with other people or Networking is a key use of LinkedIn since networking gives more room for knowledge sharing. Now we can see a way to automate linkedin connections using python

Python Automate LinkedIn connections project:

To automate LinkedIn connections using python, we make use of the library selenium.

Project Prerequisites:

The project requires certain libraries which can be installed using pip as follows:

Understanding the HTML layout of the websites will be of help to code.

Download Automating Linkedin Connections Project Code:

Please download the source code for automating linkedIn connections using python from the following link: Automate Linkedin Connections Project

Project File Structure:

Below is the flow of the Automate Linkedin Connections project.

1. Importing necessary library

2. Declaring functions to login and connect with people

3. Initialising the browser and calling functions

1. Importing libraries:

#DataFlair's Guide to Automating LinkedIn Connections using Python
#Import libraries
from selenium import webdriver

Code Explanation:

2. Declaring functions to login and connect with people:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

NOTE: To webscrape and understand the given code snippets properly, open the website in another tab and put your mouse cursor at the desired area, which can be input areas, icons etc and right click. In the menu, you will find Inspect Element or just Inspect. That is how we obtain the details for the input fields discussed below.

NOTE 2: To find the path of any element, after selecting inspect, right click on the tag the console shows to you after you select inspect. From the menu, you can choose ‘Copy Path’ and it will display the various options.

def login_to_linkedin():
#Find username
Username=browser.find_element_by_name("session_key")
#Send username details
Username.send_keys("enter username")
#Find password
password=browser.find_element_by_name("session_password")
#Send password details
password.send_keys("enter password")
#Submit button
browser.find_element_by_xpath("//button[@type='submit']").click()

Code Explanation:

def navigate_to_network_and_connect():
#Navigate to the network page
browser.find_element_by_xpath('//*[@id="ember21"]').click()
#Cause a delay so it does not throw an error
browser.implicitly_wait(5)
#Select 'See All' browser.find_element_by_xpath('/html/body/div[6]/div[3]/div/div/div/div/div[2]/div/div/main/ul/li[1]/div/button/span').click()
#Obtain the number of items containing the same path
elements = browser.find_elements_by_xpath('/html/body/div[3]/div/div/div[2]/section/div/ul/li')
for i in range(1,len(elements)+2):
#Send connection request
browser.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/section/div/ul/li[{}]/div/section/div[2]/footer/button/span'.format(str(i))).click()
#Connections send to 13 people at a time
print('Connection request sent to person {}'.format(str(i)))
#Cause a time delay before progressing to next person
browser.implicitly_wait(1)

Code Explanation:

3. Initialising the browser and calling functions:

#Open the webbrowser and use it for autonomous control
browser = webdriver.Firefox(executable_path='/home/deepika/Downloads/internship/instagram/geckodriver')
#Open the URL in the opened webbrowser
browser.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
#Start using the functions after a delay
browser.implicitly_wait(5)
#Call all the functions in order based on webpages
login_to_linkedin()
navigate_to_network_and_connect()

Code explanation:

Python Automate Linkedin Connections Output:

Run the python linkedin bot program and view your output

Summary

Thus we found a way to automate sending of connection requests to people on linkedin using python. This project is a great introduction to web scraping and is ideal for a beginner to try.

Exit mobile version