DEV Community

Manas
Manas

Posted on

Automating Linkedℹ️n Messages✉️ with LinkBot🤖

Getting bored 🥱 building the same old automation projects like login forms and basic workflows❓

It's time to level up🚀 and create an innovative and easy project using selenium and python.


Project Overview 📒

In this article we will discuss how to build an LinkedIn Message Automation Tool or script that automates the process of sending messages to multiple users, all with in just a few lines of Python code using Selenium Webdriver.


Description🗒️

LinkBot 🤖 is a Selenium-Python based project that allows to automate tedious task of sending same messages to multiple user on LinkedIn. With a simple input of login credentials and profile URLs 🔗, the bot will do the rest, saving your time and effort.


Tech Stack

Before Getting Started, here's what you'll need:

  • Selenium 🌐: To interact with the browser and perform automated tasks.
  • Python 🐍: To write automation script.

📦Setting Up the Project

First, ensure that you have following Python libraries installed, if not then install using below command in cmd.

pip install selenium


🎯Getting Started with the code

The LinkBot script automates three tasks, so we can divided the code into three functions for the following:

  1. login(): function logs into LinkedIn using credentials.
  2. send_message(): navigate to user profile and send messages.
  3. main(): contains username, password, list of users profile and a personalized message to be send.

1. Login Process

This method allows to login into LinkedIn by navigating to LinkedIn login page and entering your credentials and submitting the login form. We use Selenium's **WebDriverWait **to ensure elements are located before interacting with them.



def login(self):
    self.bot.get('https://www.linkedin.com/login')

    username_field = WebDriverWait(self.bot, 10).until(EC.presence_of_element_located((By.ID, 'username')))
    username_field.send_keys(self.username)

    password_field = WebDriverWait(self.bot, 10).until(EC.presence_of_element_located((By.ID, 'password')))
    password_field.send_keys(self.password)
    password_field.send_keys(Keys.RETURN)

    time.sleep(5)
    self.send_message()



Enter fullscreen mode Exit fullscreen mode

2. Send Messages

The send_message() method sends the message to each profile URL. For every user it does the following:

  • Opens the user's profile.

  • Waits for the message button to become clickable.

  • Types the message in message box and sends it.



def send_message(self):
    for user in self.users:
        self.bot.get(user)
        try:
            message_btn = WebDriverWait(self.bot, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="profile-content"]/div/div[2]/div/div/main/section[1]/div[2]/div[3]/div/div[1]')))
            message_btn.click()

            message_box = WebDriverWait(self.bot, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.msg-form__contenteditable')))
            message_box.send_keys(Keys.BACKSPACE)
            message_box.send_keys(self.message)

            send_btn = WebDriverWait(self.bot, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.msg-form__send-button')))
            send_btn.click()

            print(f'Message successfully sent to {user}')
        except Exception as e:
            print(f'Failed to send message to {user}: {str(e)}')



Enter fullscreen mode Exit fullscreen mode

3. The Main Function

In the main function, you simply provide your LinkedIn credentials, the list of user's profile, and the message you want to send.



def main():
    username = 'abc@gmail.com'
    password = 'abc@123'
    users = ['https://www.linkedin.com/in/user-1', 'https://www.linkedin.com/in/user-2']
    message = 'Hi there! 👋 How are you? Hope you’re doing well. This is a test message sent by LinkBot 🤖'
    bot = LinkedInBot(username, password, users, message)



Enter fullscreen mode Exit fullscreen mode

🚀How to run the Project❓

To start automating your LinkedIn messages with LinkBot, follow these steps:

  1. Cloning the repository


git clone https://github.com/Manas15-coder/LinkedIn_Message_Automation


Enter fullscreen mode Exit fullscreen mode
  1. Install the dependencies


pip install selenium



Enter fullscreen mode Exit fullscreen mode
  1. Run the script


python linkbot.py


Enter fullscreen mode Exit fullscreen mode

⭐GitHub Repository

Check out the full project on GitHub.If you find this project useful 😊, pls don't forget to give it a star ⭐on GitHub!

GitHub logo Manas15-coder / LinkedIn_Message_Automation

LinkBot 🤖, Linked In Message Automation bot, built using Selenium and Python. Using this automation script, same message ✉️ can be send to multiple users 👦, just need to input the user profile url 🔗, and in this way tedious task of sending messages to individuals can be automated 💻.

🔐 LinkBot 🤖: LinkedIn Message Automation

Automating linkedIn messages, so that same message ✉️ can be send to multiple users 👦, just need to input the user profile url 🔗, and in this way tedious task of sending messages to individuals can be automated 💻.

🚀 Tech Stack

  • Python 🐍
  • Selenium WebDriver 🌐





Top comments (0)