Send Direct Message On Instagram using Selenium in Python - GeeksforGeeks

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.
Chrome Driver for Chrome Browser (https://chromedriver.chromium.org/) or Gecko Driver for Firefox(https://github.com/mozilla/geckodriver/releases)Selenium Package. To install this type the below command in the terminal.
Chrome Driver for Chrome Browser (https://chromedriver.chromium.org/) or Gecko Driver for Firefox(https://github.com/mozilla/geckodriver/releases)
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
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport selenium.common.exceptionsimport timeimport random # Login Credentialsusername = input('Enter your Username ')password = input('Enter your Password ')url = 'https://instagram.com/' + input('Enter username of user whome you want to send message')
  
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
def path():        global chrome             # starts a new chrome session        chrome = webdriver.Chrome() # Add path if required
  
Step 3: Function to enter the URL of the page
 
Python3
def url_name(url):  chrome.get(url)     # adjust sleep if you want  time.sleep(4)
Step 4: Function to login to Instagram  
Python3
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)
  
Step 5: Find the message Button on the User profile page and then send random messages to the user
 
Python3
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)
  
Step 6: Calling Functions
 
Python3
path()time.sleep(1)url_name(url)login(username, password)send_message()chrome.close()
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:
UnworthyProgrammer
Python Selenium-Exercises
Python-selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | os.path.join() method
Python | Get unique values from a list
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()