HTML

    Select a Subtopic

    📚 Day 18: Advanced Instagram Bot Automation (Selenium + Tkinter)

    Today, we'll upgrade your Instagram bot with more powerful features:

    • Automated commenting on posts.
    • Following users.
    • Dynamic interaction based on hashtags.
    • Adding a "Stop Bot" feature to pause the bot anytime.

    Let's make your bot more smart, interactive, and user-friendly!


    What You'll Learn Today:

    • Automating comments on Instagram posts.
    • Automating follow/unfollow actions.
    • Using hashtags to interact with posts dynamically.
    • Adding a Stop Button in Tkinter to stop the bot.

    🧑‍💻 1. Automating Comments on Instagram Posts

    We'll make the bot search for a specific hashtag and comment on the first few posts.

    Updated `instagram_login()` Function:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time
    
    # Function to log in and comment on posts
    def instagram_login(username, password, comment_text):
        driver = webdriver.Chrome(executable_path="path/to/chromedriver")
        driver.get("https://www.instagram.com/accounts/login/")
        time.sleep(5)
    
        # Enter username and password
        driver.find_element(By.NAME, "username").send_keys(username)
        driver.find_element(By.NAME, "password").send_keys(password)
        driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click()
        time.sleep(5)
    
        # Search for a hashtag
        driver.get("https://www.instagram.com/explore/tags/programming/")
        time.sleep(5)
    
        # Click on the first post
        driver.find_element(By.CLASS_NAME, '_aagw').click()
        time.sleep(3)
    
        # Add a comment
        driver.find_element(By.XPATH, "//textarea").send_keys(comment_text)
        driver.find_element(By.XPATH, "//button[contains(text(), 'Post')]").click()
        print("Commented on the post!")
        time.sleep(3)
    
        driver.quit()

    🎯 2. Automating Follow/Unfollow Actions

    You can also automate the process of following or unfollowing users.

    Follow Users Code:

    # Follow a user after login
    def follow_user(username_to_follow):
        search_url = f"https://www.instagram.com/{username_to_follow}/"
        driver.get(search_url)
        time.sleep(5)
    
        follow_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Follow')]")
        follow_button.click()
        print(f"Followed {username_to_follow}!")

    🖥 3. Tkinter GUI with "Start" and "Stop" Buttons

    Let's add start and stop buttons to your Tkinter GUI to control the bot.

    Complete Tkinter GUI Code:

    import tkinter as tk
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time
    import threading
    
    # Global variable for the driver
    driver = None
    
    # Function to start the bot
    def start_bot(username, password, comment_text):
        global driver
        driver = webdriver.Chrome(executable_path="path/to/chromedriver")
        driver.get("https://www.instagram.com/accounts/login/")
        time.sleep(5)
    
        driver.find_element(By.NAME, "username").send_keys(username)
        driver.find_element(By.NAME, "password").send_keys(password)
        driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click()
        time.sleep(5)
    
        # Navigate to a hashtag and comment
        driver.get("https://www.instagram.com/explore/tags/programming/")
        time.sleep(5)
    
        driver.find_element(By.CLASS_NAME, '_aagw').click()
        time.sleep(3)
        driver.find_element(By.XPATH, "//textarea").send_keys(comment_text)
        driver.find_element(By.XPATH, "//button[contains(text(), 'Post')]").click()
        print("Commented on the post!")
    
    # Function to stop the bot
    def stop_bot():
        global driver
        if driver:
            driver.quit()
            print("Bot stopped!")
    
    # Create the GUI
    root = tk.Tk()
    root.title("Instagram Bot")
    root.geometry("400x300")
    
    username_label = tk.Label(root, text="Username:")
    username_label.grid(row=0, column=0, padx=10, pady=10)
    username_entry = tk.Entry(root)
    username_entry.grid(row=0, column=1, padx=10, pady=10)
    
    password_label = tk.Label(root, text="Password:")
    password_label.grid(row=1, column=0, padx=10, pady=10)
    password_entry = tk.Entry(root, show="*")
    password_entry.grid(row=1, column=1, padx=10, pady=10)
    
    comment_label = tk.Label(root, text="Comment:")
    comment_label.grid(row=2, column=0, padx=10, pady=10)
    comment_entry = tk.Entry(root)
    comment_entry.grid(row=2, column=1, padx=10, pady=10)
    
    start_button = tk.Button(root, text="Start Bot", command=lambda: threading.Thread(target=start_bot, args=(username_entry.get(), password_entry.get(), comment_entry.get())).start())
    start_button.grid(row=3, column=0, pady=20)
    
    stop_button = tk.Button(root, text="Stop Bot", command=stop_bot)
    stop_button.grid(row=3, column=1, pady=20)
    
    # Run the GUI loop
    root.mainloop()

    🎨 4. Enhancing the GUI with Styles

    You can make the GUI more attractive by using fonts, colors, and layouts.

    Style the Buttons:

    start_button = tk.Button(root, text="Start Bot", font=("Helvetica", 14), bg="green", fg="white", command=lambda: threading.Thread(target=start_bot, args=(username_entry.get(), password_entry.get(), comment_entry.get())).start())
    stop_button = tk.Button(root, text="Stop Bot", font=("Helvetica", 14), bg="red", fg="white", command=stop_bot)

    🎯 Task for Today:

    • Add commenting functionality.
    • Add follow/unfollow actions.
    • Implement a Stop button in the GUI.
    • Test the bot using different hashtags.

    Would you like me to guide you through Day 19 with more advanced features like automated DM sending and Instagram scraping? 😊