In this Post, We will see how to send messeges on instagram using Python.
For this Purpose, we will be using Selenium Web Driver.
Before we get started, you may like to read our post to get list of non followers who have interected on your posts on instagram, you can apply this “sending Messeges script” on that list for marketing purpose.
Before you get the code , Follow me on Instagram to get more content like this 🙂
Code to send Messeges on Instagram
Below is code for Sending messeges on instagram using Python
# 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
#it will get your version of chrome and will install web driver suitable to your version.
driver = webdriver.Chrome(ChromeDriverManager().install())
# enter receiver user name
user = ['username1', 'username2','code_seekers']
message_ = ("This is a Test messege, you can replace it with your custom marketing messege.")
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(
'//*[@id="react-root"]/section/main/div/div/div/div/button').click()
time.sleep(3)
# 2nd pop-up
self.bot.find_element_by_xpath(
'/html/body/div[5]/div/div/div/div[3]/button[2]').click()
time.sleep(4)
# direct button
self.bot.find_element_by_xpath(
'//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[2]/a').click()
time.sleep(3)
# clicks on pencil icon
self.bot.find_element_by_xpath(
'//*[@id="react-root"]/section/div/div[2]/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[6]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(i)
time.sleep(2)
# click on the username
self.bot.find_element_by_xpath(
'/html/body/div[6]/div/div/div[2]/div[2]/div[1]/div/div[3]/button').click()
time.sleep(2)
# next button
self.bot.find_element_by_xpath(
'/html/body/div[6]/div/div/div[1]/div/div[2]/div/button').click()
time.sleep(2)
# click on message area
send = self.bot.find_element_by_xpath(
'//*[@id="react-root"]/section/div/div[2]/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 pencl icon
self.bot.find_element_by_xpath(
'//*[@id="react-root"]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()
time.sleep(2)
def init():
bot('username', 'password', user, message_)
# when our program ends it will show "done".
print("DONE")
# calling the function
init()