Related Articles

Related Articles

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.

Requirements:

  1. Chrome Driver for Chrome Browser (https://chromedriver.chromium.org/) or Gecko Driver for Firefox(https://github.com/mozilla/geckodriver/releases)
  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 

Approach:

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

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

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 = 'https://instagram.com/' + input('Enter username of user whome you want to send message')

chevron_right


 
 

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.

 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 
 

Step 3: Function to enter the URL of the page

 

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

def url_name(url):
  chrome.get(url)
   
  # adjust sleep if you want
  time.sleep(4)

chevron_right


Step 4: Function to login to Instagram  

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def login(username, your_password):
    log_but = chrome.find_element_by_class_name("L3NKy")
    time.sleep(2)
    log_but.click()
    time.sleep(4)
     
    # finds the username box
    usern = chrome.find_element_by_name("username")
     
    # sends the entered username
    usern.send_keys(username)
 
    # finds the password box
    passw = chrome.find_element_by_name("password")
 
    # sends the entered password
    passw.send_keys(your_password)
     
    # press enter after sending password
    passw.send_keys(Keys.RETURN)
    time.sleep(5.5)
     
    # Finding Not Now button
    notk = chrome.find_element_by_class_name("yWX7d"
    notk.click()
    time.sleep(3)

chevron_right


 
 

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

 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def send_message():
   
    # Find message button
    message = chrome.find_element_by_class_name('_862NM ')
    message.click()
    time.sleep(2)
    chrome.find_element_by_class_name('HoLwm ').click()
    time.sleep(1)
    l = ['hello', 'Hi', 'How are You', 'Hey', 'Bro whats up']
    for x in range(10):
        mbox = chrome.find_element_by_tag_name('textarea')
        mbox.send_keys(random.choice(l))
        mbox.send_keys(Keys.RETURN)
        time.sleep(1.2)

chevron_right


 
 

Step 6: Calling Functions

 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

path()
time.sleep(1)
url_name(url)
login(username, password)
send_message()
chrome.close()

chevron_right


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.

Output:


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :