HTML

    Select a Subtopic

    πŸš€ **Day 11: Scrolling and Loading More Content with Selenium**

    Instagram's feed and hashtag pages load content dynamically as you scroll. The goal today is to automate infinite scrolling and ensure new content loads.

    πŸ—“οΈ **Day 11 Focus: Automating Scrolling and Loading More Content**

    Today, we’ll focus on:

    • βœ… Using execute_script() to scroll.
    • βœ… Detecting when new content loads.
    • βœ… Interacting with dynamic elements after scrolling.
    • βœ… Automating the process to like posts as you scroll.

    πŸ’» **Code Walkthrough: Scrolling on Instagram Hashtag Page**

    🧩 **Step 1: Set Up Your Environment**

    Make sure you have Selenium installed and the Chrome WebDriver configured.

    pip install selenium

    🧩 **Step 2: Basic Script to Open Instagram Hashtag Page**

    Here’s a starter script to open Instagram and scroll through a hashtag page.

    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 and navigate to a hashtag page
    driver.get("https://www.instagram.com/explore/tags/coding/")
    
    # Wait for the page to load
    time.sleep(5)
    
    # Infinite scroll loop
    for _ in range(5):  # Adjust the range for more scrolling
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(2)  # Adjust the sleep time based on your internet speed

    🧩 **Step 3: Detecting New Posts and Interacting with Them**

    Let’s modify the script to like posts as new ones load.

    # Find and like the first 10 posts
    posts = driver.find_elements(By.CLASS_NAME, "_aagw")  # Instagram post class
    for index, post in enumerate(posts[:10]):
        post.click()
        time.sleep(2)
        
        # Locate and click the like button
        try:
            like_button = driver.find_element(By.XPATH, "//span[@aria-label='Like']")
            like_button.click()
            print(f"Post {index + 1} liked!")
        except:
            print(f"Post {index + 1} was already liked.")
        
        # Close the post
        close_button = driver.find_element(By.XPATH, "//div[contains(@class,'_abl-')]")
        close_button.click()
        time.sleep(2)

    πŸ§ͺ **Practical Task for Day 11**

    βœ… **Goal:**

    • Scroll through the Instagram hashtag page.
    • Like 10 new posts that load as you scroll.

    ⚠️ **Important Tips for Avoiding Detection**

    • **Randomize your scrolling intervals:** Use time.sleep() with random delays.
      import random
      time.sleep(random.uniform(2, 5))
    • **Mimic Human Behavior:** Scroll slowly and avoid repetitive actions.

    Would you like me to prepare **Day 12** as well? 😊