Automate Instagram Messages
using Python
In this article, we will see how to send a single message to any
number of people. We just have to provide a list of users. We
will use selenium for this task.
Packages needed
Selenium: It is an open-source tool that automates web
browsers. It provides a single interface that lets you write
test scripts in programming languages like Ruby, Java,
NodeJS, PHP, Perl, Python, and C#, among others. I
personally prefer Python as it’s very easy to write code in
python. A browser-driver then executes these scripts on a
browser-instance on your device. To install this module run
this command into your terminal.
pip install selenium
webdriver-manager: It provides a way to automatically
manage drivers for different browsers. To install this module
run this command into your terminal.
pip install webdriver-manager
ChromeDriver: ChromeDriver is a separate executable that
Selenium WebDriver uses to control Chrome.
Google Chrome
Source Code:-
# importing module
from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import
WebDriverWait
from selenium.webdriver.support import
expected_conditions
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import
ChromeDriverManager
driver =
webdriver.Chrome(ChromeDriverManager().install()
)
# enter receiver user name
user = ['User_name', 'User_name ']
message_ = ("final test")
class bot:
def __init__(self, username, password, user,
message):
self.username = username
self.password = password
self.user = user
self.message = message
self.base_url = 'https://www.instagram.com/'
self.bot = driver
self.login()
def login(self):
self.bot.get(self.base_url)
enter_username = WebDriverWait(self.bot,
20).until(
expected_conditions.presence_of_element_located(
(By.NAME, 'username')))
enter_username.send_keys(self.username)
enter_password = WebDriverWait(self.bot,
20).until(
expected_conditions.presence_of_element_located(
(By.NAME, 'password')))
enter_password.send_keys(self.password)
enter_password.send_keys(Keys.RETURN)
time.sleep(5)
# first pop-up
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1
]/div[1]/div[2]/section/main/div/div/div/div/button').
click()
time.sleep(5)
# 2nd pop-up
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/di
v/div[2]/div/div/div/div/div[2]/div/div/div[3]/button[
2]').click()
time.sleep(5)
# direct button
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1
]/div[1]/div[1]/div/div/div/div/div[2]/div[4]/div/a').cli
ck()
time.sleep(3)
# clicks on pencil icon
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1
]/div[1]/div/div[2]/div/section/div/div/div/div/div[1]/
div[1]/div/div[3]/button').click()
time.sleep(2)
for i in user:
# enter the username
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/di
v/div[2]/div/div/div/div/div[2]/div/div[2]/div[1]/div/di
v[2]/input').send_keys(i)
time.sleep(3)
# click on the username
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/di
v/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div[1
]/div/div[3]/button').click()
time.sleep(4)
# next button
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/di
v/div[2]/div/div/div/div/div[2]/div/div[1]/div/div[3]/d
iv/button').click()
time.sleep(4)
# click on message area
send = self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1
]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/
div[2]/div/div[2]/div/div/div[2]/textarea')
# types message
send.send_keys(self.message)
time.sleep(1)
# send message
send.send_keys(Keys.RETURN)
time.sleep(2)
# clicks on direct option or pencil icon
self.bot.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1
]/div[1]/div/div[2]/div/section/div/div/div/div/div[1]/
div[1]/div/div[3]/button').click()
time.sleep(4)
def init():
bot('username', 'password', user, message_)
# when our program ends it will show "done".
input("DONE")
# calling the function
init()