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
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
- Ethical Use: Automating social media interactions should comply with platform policies. Avoid spamming or using this script for malicious purposes.
- Chrome Version: Ensure your Chrome browser is updated to match the ChromeDriver version managed by WebDriver Manager.
- 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 Comment