HTML

    Select a Subtopic

    🔥 Day 12: Commenting on Posts

    What You’ll Learn Today:

    • Locate the Comment field using XPath or CSS selectors.
    • Post a comment on the first few posts of a profile or hashtag page.
    • Mimic human-like behavior by introducing random delays to avoid detection.

    🧑‍💻 Step 1: Setup & Login to Instagram

    We’ll start by logging in to Instagram using Selenium.

    🖥️ Code:

    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    import time
    
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--disable-notifications")
    chrome_options.add_argument("--start-maximized")
    
    # Initialize the driver
    service = Service("path/to/chromedriver")
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    # Open Instagram
    driver.get("https://www.instagram.com/")
    time.sleep(5)  # Wait for the page to load
    
    # Login
    username = driver.find_element(By.NAME, "username")
    password = driver.find_element(By.NAME, "password")
    username.send_keys("your_username")
    password.send_keys("your_password")
    driver.find_element(By.XPATH, "//button[@type='submit']").click()
    
    time.sleep(5)  # Wait for login to complete
            

    🧑‍💻 Step 2: Navigate to a Profile or Hashtag Page

    Let’s navigate to a specific hashtag page to interact with posts.

    🖥️ Code:

    
    # Navigate to a hashtag page
    driver.get("https://www.instagram.com/explore/tags/nature/")
    time.sleep(5)
            

    🧑‍💻 Step 3: Locate and Open a Post

    Identify the first post on the hashtag page and click on it.

    🖥️ Code:

    
    # Click on the first post
    first_post = driver.find_element(By.XPATH, "//article//a")
    first_post.click()
    time.sleep(3)
            

    🧑‍💻 Step 4: Post a Comment

    We’ll now locate the Comment field, type a comment, and click Post.

    🖥️ Code:

    
    # Find the comment field
    comment_box = driver.find_element(By.XPATH, "//textarea[@aria-label='Add a comment…']")
    
    # Type a comment
    comment_box.send_keys("Nice post! 😊")
    
    # Click the Post button
    post_button = driver.find_element(By.XPATH, "//button[text()='Post']")
    post_button.click()
    time.sleep(2)
            

    🧑‍💻 Step 5: Automate Commenting on Multiple Posts

    Use a loop to comment on multiple posts.

    🖥️ Code:

    
    # Loop through multiple posts
    for _ in range(3):  # Adjust the range as needed
        comment_box = driver.find_element(By.XPATH, "//textarea[@aria-label='Add a comment…']")
        comment_box.send_keys("Amazing content! 🔥")
        
        post_button = driver.find_element(By.XPATH, "//button[text()='Post']")
        post_button.click()
        time.sleep(2)
        
        # Navigate to the next post
        next_button = driver.find_element(By.XPATH, "//a[contains(text(), 'Next')]")
        next_button.click()
        time.sleep(3)
            

    🛠️ Practical Task for Today:

    • Create a bot that comments on the first 3 posts of a hashtag page.
    • Randomize the comments and introduce random delays to mimic human behavior.

    🚀 Bonus Tips:

    Use time.sleep() with random intervals using the random module:

    
    import random
    time.sleep(random.randint(2, 5))
            

    Would you like me to provide a full project file with structured code? 😊