Automate LinkedIn Outreach with Python and Selenium

LInkedIn Outreach

Table of Contents

Introduction

Automating LinkedIn outreach can help you efficiently manage your connections and grow your professional network. This tutorial will guide you through creating a Python script using Selenium to automate sending connection requests and messages on LinkedIn.

Requirements

  • Basic knowledge of Python
  • Python installed on your system
  • Selenium library installed (pip install selenium)
  • ChromeDriver or a compatible web driver for your browser
  • A text editor (e.g., VSCode, PyCharm)

Step 1: Installing Required Python Libraries

Install the necessary Python libraries using pip:

pip install selenium

Step 2: Setting Up Selenium and ChromeDriver

  1. Download ChromeDriver: Go to the ChromeDriver download page and download the version that matches your Chrome browser version.
  2. Set Up ChromeDriver: Place the chromedriver executable in a directory that’s in your system’s PATH or in the same directory as your Python script.

Step 3: Writing the Python Script

Import Libraries: Create a Python script named linkedin_bot.py and start by importing the necessary libraries:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the Chrome driver
driver = webdriver.Chrome('path/to/chromedriver')

# Define LinkedIn login credentials
username = 'your_username'
password = 'your_password'

# Open LinkedIn
driver.get('https://www.linkedin.com')
time.sleep(2)

Login to LinkedIn:

def login(username, password):
    # Find and fill the username field
    user_input = driver.find_element_by_name('session_key')
    user_input.send_keys(username)
    time.sleep(1)
    
    # Find and fill the password field
    pass_input = driver.find_element_by_name('session_password')
    pass_input.send_keys(password)
    time.sleep(1)
    
    # Press the login button
    pass_input.send_keys(Keys.RETURN)
    time.sleep(5)

login(username, password)

Replace 'your_username' and 'your_password' with your actual LinkedIn login credentials.

Automating Connection Requests:

def connect_with_users():
    # Go to the LinkedIn search page
    driver.get('https://www.linkedin.com/search/results/people/')
    time.sleep(2)

    # Loop through the first few profiles and send connection requests
    for _ in range(10):
        connect_buttons = driver.find_elements_by_xpath("//button[text()='Connect']")
        for button in connect_buttons:
            button.click()
            time.sleep(1)
            send_button = driver.find_element_by_xpath("//button[@aria-label='Send now']")
            send_button.click()
            time.sleep(1)

connect_with_users()

Sending Messages:

def send_messages():
    # Go to the LinkedIn messages page
    driver.get('https://www.linkedin.com/messaging/')
    time.sleep(2)

    # Loop through the first few conversations and send messages
    conversations = driver.find_elements_by_class_name('msg-conversation-listitem')
    for conversation in conversations[:5]:
        conversation.click()
        time.sleep(1)
        message_box = driver.find_element_by_class_name('msg-form__contenteditable')
        message_box.send_keys('Hello! This is an automated message.')
        message_box.send_keys(Keys.RETURN)
        time.sleep(1)

send_messages()

Running the Script:

if __name__ == '__main__':
    login(username, password)
    connect_with_users()
    send_messages()
    driver.quit()

Step 4: Running the Script

Run your Python script using the following command:

python linkedin_bot.py

Ensure that the browser window remains open and the internet connection is stable during the script’s execution.

Conclusion

Automating LinkedIn outreach with Python and Selenium can streamline your networking efforts and help you maintain an active presence on the platform. This tutorial provided a step-by-step guide to setting up and running a simple LinkedIn bot.

Try
RecurPost

Schedule and Publish your posts on multiple social accounts, read and reply to incoming messages with social inbox, and collaborate with your team and clients with ease.

Scroll to Top