Select a Subtopic
🚀 Day 9: Automating Likes and Follows on Instagram using Selenium
In today's lesson, you'll learn how to automate liking posts and following users on Instagram using Selenium. This involves locating dynamic elements like the Like and Follow buttons and interacting with them in a loop.
🎯 What You'll Learn Today
- ✅ Locate and interact with Like and Follow buttons.
- ✅ Handle dynamic content using for loops and if conditions.
- ✅ Add random delays to mimic human behavior.
- ✅ Implement a simple bot to like posts on a hashtag page.
🖥️ Step-by-Step Guide
🛠️ 1. Setting Up Your Project
Make sure you have:
- - Python installed.
- - Selenium installed using:
pip install selenium
- - ChromeDriver downloaded and set up.
🔍 2. Locating Like and Follow Buttons Using XPath
Open Instagram, log in, and visit a hashtag page (e.g., https://www.instagram.com/explore/tags/python/
). Inspect the Like button using Chrome Developer Tools:
- Right-click on the heart icon and click Inspect.
- Find the XPath for the button.
Example XPath for the Like button:
Similarly, find the Follow button:
💻 3. Writing Code to Automate Likes
Here's how to write a bot to like the first 5 posts on a hashtag page:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service import time # Set up the WebDriver service = Service("path/to/chromedriver") driver = webdriver.Chrome(service=service) # Open Instagram and log in driver.get("https://www.instagram.com/") time.sleep(3) # Wait for the page to load # Enter credentials driver.find_element(By.NAME, "username").send_keys("your_username") driver.find_element(By.NAME, "password").send_keys("your_password") driver.find_element(By.XPATH, "//button[@type='submit']").click() time.sleep(5) # Wait for login to complete # Navigate to a hashtag page driver.get("https://www.instagram.com/explore/tags/python/") time.sleep(5) # Find and like the first 5 posts posts = driver.find_elements(By.XPATH, "//a[contains(@href, '/p/')]") for i in range(5): posts[i].click() time.sleep(2) # Click the Like button like_button = driver.find_element(By.XPATH, "//button[@aria-label='Like']") like_button.click() time.sleep(2) # Close the post driver.find_element(By.XPATH, "//button[@aria-label='Close']").click() time.sleep(2) # Close the browser driver.quit()
🤖 4. Automating Follows
To automate following users on a profile page:
# Visit a profile page driver.get("https://www.instagram.com/username/") time.sleep(3) # Click the Follow button if it exists try: follow_button = driver.find_element(By.XPATH, "//button[text()='Follow']") follow_button.click() print("Followed the user!") except: print("Already following or button not found.")
⏱️ 5. Adding Random Delays to Mimic Human Behavior
Instagram may detect repetitive, fast actions as bot activity. To avoid this, add random delays:
import random # Random delay between actions time.sleep(random.randint(3, 10))
✅ 6. Task for Today
Create a bot that:
- 1. Logs into Instagram.
- 2. Navigates to a hashtag page of your choice.
- 3. Likes the first 5 posts.
- 4. Follows the profile of the post author.
⚙️ Next Steps
Tomorrow, we'll explore headless mode, user-agent modifications, and proxy settings to avoid detection.
Would you like me to share more advanced tips to make your bot undetectable? 😊