Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Automate Instagram Messages Using Python - Automate Instagram Messages in Python

Automate Instagram Messages Using Python

Posted on December 18, 2024December 18, 2024 By Rishabh saini No Comments on Automate Instagram Messages Using Python

Automate Instagram Messages Using Python

Have you ever thought about automating repetitive Instagram tasks, like sending messages to multiple users? With Python, this is entirely possible! In this article, we’ll guide you through automating Instagram messaging using Selenium, a robust tool for web browser automation. By the end of this guide, you’ll know how to send a single message to a list of Instagram users effortlessly.

Download New Real Time Projects :-Click here


Automate Instagram Messages Using Python
Automate Instagram Messages Using Python

What You’ll Need

To automate Instagram messages, we’ll use the following tools:

1. Selenium

Selenium is an open-source tool for automating web browsers. It supports multiple programming languages, including Python. Install it by running:

pip install selenium

2. WebDriver Manager

This module helps manage browser drivers like ChromeDriver automatically. Install it by running:

pip install webdriver-manager

3. ChromeDriver

ChromeDriver is required for Selenium to interact with Google Chrome. It’s automatically managed by WebDriver Manager.


Step-by-Step Guide

Step 1: Import Necessary Libraries

Start by importing all the required modules:

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager

Step 2: Set Up the WebDriver

Using WebDriver Manager ensures the ChromeDriver is always up to date:

driver = webdriver.Chrome(ChromeDriverManager().install())

Step 3: Define Users and Message

Create a list of Instagram usernames and the message you want to send:

# List of Instagram usernames
audience = ['sundarpichai', 'virat.kohli', 'rudymancuso']

# Message to be sent
message = "Hello! This is an automated message using Python."

Step 4: Create a Bot Class

Encapsulate the automation logic in a class for better organization:

class InstagramBot:
    def __init__(self, username, password, audience, message):
        self.username = username
        self.password = password
        self.audience = audience
        self.message = message
        self.base_url = "https://www.instagram.com/"
        self.driver = driver
        self.login()

    def login(self):
        # Open Instagram login page
        self.driver.get(self.base_url)

        # Log in to Instagram
        WebDriverWait(self.driver, 20).until(
            EC.presence_of_element_located((By.NAME, "username"))
        ).send_keys(self.username)
        WebDriverWait(self.driver, 20).until(
            EC.presence_of_element_located((By.NAME, "password"))
        ).send_keys(self.password + Keys.RETURN)
        time.sleep(5)

        # Handle pop-ups
        self.handle_popups()

    def handle_popups(self):
        # Click 'Not Now' for saving login info
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//button[text()="Not Now"]'))
        ).click()
        time.sleep(2)

        # Click 'Not Now' for notifications
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//button[text()="Not Now"]'))
        ).click()
        time.sleep(2)

    def send_messages(self):
        # Open Direct Messages
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//a[contains(@href, "direct/inbox")]'))
        ).click()
        time.sleep(3)

        # Send messages to all users
        for user in self.audience:
            # Click the 'New Message' button
            self.driver.find_element(By.XPATH, '//button[text()="Send Message"]').click()
            time.sleep(2)

            # Search for the user
            search_box = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Search..."]'))
            )
            search_box.send_keys(user)
            time.sleep(2)

            # Select the user
            self.driver.find_element(By.XPATH, f'//div[text()="{user}"]').click()
            time.sleep(2)

            # Click 'Next'
            self.driver.find_element(By.XPATH, '//button[text()="Next"]').click()
            time.sleep(2)

            # Type and send the message
            message_box = self.driver.find_element(By.XPATH, '//textarea')
            message_box.send_keys(self.message)
            message_box.send_keys(Keys.RETURN)
            time.sleep(2)

        print("Messages sent successfully!")

Step 5: Execute the Bot

Finally, create a function to initialize the bot with your credentials:

def main():
    username = "your_instagram_username"
    password = "your_instagram_password"
    bot = InstagramBot(username, password, audience, message)
    bot.send_messages()

if __name__ == "__main__":
    main()

Important Notes

  1. Ethical Use: Automating social media interactions should comply with platform policies. Avoid spamming or using this script for malicious purposes.
  2. Chrome Version: Ensure your Chrome browser is updated to match the ChromeDriver version managed by WebDriver Manager.
  3. Dynamic Elements: Instagram frequently updates its UI, which may change the XPath or elements used in the script. Regular updates may be necessary.

PHP PROJECT:- CLICK HERE


send instagram message using python
instabot python
Automate Instagram Messages Using Python
instagram dm bot python
instagram api
Automate Instagram Messages
send whatsapp message using python
automate google search using python
automate linkedin connections using python
instagram dm bot github
automate instagram messages using python github

Post Views: 880
code Snippets Tags:automate instagram, automate instagram message using python, automate instagram messaging, automate instagram outreach, automate instagram using python, automate whatsapp messages python, automate whatsapp messages using python, automating instagram messages, instagram, instagram automation, instagram automation with python, instagram bot, instagram bot python, instagram python, Python, python instagram, python instagram api, python instagram bot

Post navigation

Previous Post: Chess Game in Python with Source Code
Next Post: Database Connection To Python Applications

More Related Articles

Supplier Management System in Java with Free Code - Supplier Management system Supplier Management System in Java with Free Code code Snippets
Finding the Sum of Digits Finding the Sum of Digits: Multi language Java, Python, Js code Snippets
Online Food Ordering System in Python with Source Code - How To Online Food Ordering System in Python with Source Code code Snippets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. News Portal Project in PHP and MySql Free Source Code
  5. Flipkart Clone using PHP And MYSQL Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme