Building a Custom Instagram Bot with Python and Selenium

Instagram Bot

Table of Contents

Introduction

An Instagram bot can help automate various tasks such as liking posts, following users, and commenting, which can save you time and increase your engagement. This tutorial will guide you through creating a custom Instagram bot using Python and Selenium.

Requirements

  • Basic knowledge of Python
  • Python installed on your system
  • Selenium library installed (pip install selenium)
  • ChromeDriver or a compatible web driver for your browser
  • A text editor (e.g., VSCode, PyCharm)

Step 1: Installing Required Libraries

Install the necessary Python libraries using pip:

pip install selenium

Step 2: Setting Up Selenium and ChromeDriver

  1. Download ChromeDriver: Go to the ChromeDriver download page and download the version that matches your Chrome browser version.
  2. Set Up ChromeDriver: Place the chromedriver executable in a directory that’s in your system’s PATH or in the same directory as your Python script.

Step 3: Writing the Python Script

Create a Python script named instagram_bot.py and start by importing the necessary modules and setting up the Selenium WebDriver:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the Chrome driver
driver = webdriver.Chrome('path/to/chromedriver')

# Define Instagram login credentials
username = 'your_username'
password = 'your_password'

# Open Instagram
driver.get('https://www.instagram.com')
time.sleep(2)

# Function to log in to Instagram
def login(username, password):
    # Find and fill the username field
    user_input = driver.find_element_by_name('username')
    user_input.send_keys(username)
    time.sleep(1)
    
    # Find and fill the password field
    pass_input = driver.find_element_by_name('password')
    pass_input.send_keys(password)
    time.sleep(1)
    
    # Press the login button
    pass_input.send_keys(Keys.RETURN)
    time.sleep(5)

login(username, password)

Replace 'your_username' and 'your_password' with your actual Instagram login credentials.

Step 4: Automating Basic Actions

  1. Liking Posts: Add the following function to like posts on the home feed:
def like_posts(amount):
    driver.get('https://www.instagram.com')
    time.sleep(2)
    
    post = driver.find_element_by_class_name('v1Nh3')
    
    for _ in range(amount):
        post.click()
        time.sleep(1)
        like_button = driver.find_element_by_xpath('//span[@aria-label="Like"]')
        like_button.click()
        time.sleep(1)
        close_button = driver.find_element_by_xpath('//button[@aria-label="Close"]')
        close_button.click()
        time.sleep(1)
        post = driver.find_element_by_class_name('v1Nh3')

like_posts(5)  # Like 5 posts
  1. Following Users: Add the following function to follow users from a specific hashtag:
def follow_users_from_hashtag(hashtag, amount):
    driver.get(f'https://www.instagram.com/explore/tags/{hashtag}/')
    time.sleep(2)
    
    post = driver.find_element_by_class_name('v1Nh3')
    post.click()
    time.sleep(1)
    
    for _ in range(amount):
        follow_button = driver.find_element_by_xpath('//button[text()="Follow"]')
        follow_button.click()
        time.sleep(2)
        next_button = driver.find_element_by_xpath('//a[contains(text(),"Next")]')
        next_button.click()
        time.sleep(2)

follow_users_from_hashtag('python', 5)  # Follow 5 users from the #python hashtag
  1. Commenting on Posts: Add the following function to comment on posts from a specific hashtag:
def comment_on_posts(hashtag, comment, amount):
    driver.get(f'https://www.instagram.com/explore/tags/{hashtag}/')
    time.sleep(2)
    
    post = driver.find_element_by_class_name('v1Nh3')
    post.click()
    time.sleep(1)
    
    for _ in range(amount):
        comment_button = driver.find_element_by_xpath('//span[@aria-label="Comment"]')
        comment_button.click()
        time.sleep(1)
        comment_input = driver.find_element_by_xpath('//textarea[@aria-label="Add a comment…"]')
        comment_input.send_keys(comment)
        comment_input.send_keys(Keys.RETURN)
        time.sleep(2)
        next_button = driver.find_element_by_xpath('//a[contains(text(),"Next")]')
        next_button.click()
        time.sleep(2)

comment_on_posts('python', 'Great post!', 5)  # Comment on 5 posts with the #python hashtag

Conclusion

Building a custom Instagram bot with Python and Selenium can automate repetitive tasks such as liking posts, following users, and commenting. This tutorial provided a step-by-step guide to set up and run a simple Instagram bot, which can be extended with more features as needed.

Try
RecurPost

Schedule and Publish your posts on multiple social accounts, read and reply to incoming messages with social inbox, and collaborate with your team and clients with ease.

Scroll to Top