Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Send Direct Message On Instagram using Selenium in Python

  • Difficulty Level : Medium
  • Last Updated : 04 Jan, 2021

In this article, we will learn how we can send a direct message to users on Instagram without any manual action. We will be using the selenium module to do this task.


  1. Chrome Driver for Chrome Browser ( or Gecko Driver for Firefox(
  2. Selenium Package. To install this type the below command in the terminal.
pip install selenium

Note: For more information, refer How to install Selenium in Python 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


Step 1: Importing modules and entering the login information along with the username of the user whom you want to send a message.


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.common.exceptions
import time
import random
# Login Credentials
username = input('Enter your Username ')
password = input('Enter your Password ')
url = '' + input('Enter username of user whome you want to send message')


Step 2: Function to initialize Firefox or chrome session. You might need to add the path to the web driver. Chrome function, it depends on your installation.



def path():
        global chrome
        # starts a new chrome session
        chrome = webdriver.Chrome() # Add path if required


Step 3: Function to enter the URL of the page



def url_name(url):
  # adjust sleep if you want

Step 4: Function to login to Instagram  


def login(username, your_password):
    log_but = chrome.find_element_by_class_name("L3NKy")
    # finds the username box
    usern = chrome.find_element_by_name("username")
    # sends the entered username
    # finds the password box
    passw = chrome.find_element_by_name("password")
    # sends the entered password
    # press enter after sending password
    # Finding Not Now button
    notk = chrome.find_element_by_class_name("yWX7d"


Step 5: Find the message Button on the User profile page and then send random messages to the user



def send_message():
    # Find message button
    message = chrome.find_element_by_class_name('_862NM ')
    chrome.find_element_by_class_name('HoLwm ').click()
    l = ['hello', 'Hi', 'How are You', 'Hey', 'Bro whats up']
    for x in range(10):
        mbox = chrome.find_element_by_tag_name('textarea')


Step 6: Calling Functions



login(username, password)

That’s it! This script will automatically send messages to your loved one. You can do a lot by modifying this script like scheduling automatic messages, sending messages to bulk users and many more.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!