HTML

    Select a Subtopic

    πŸš€ Day 5: Navigating Instagram and Handling Pop-Ups with Selenium

    Today, we'll take your Instagram automation to the next level by learning:

    • βœ… How to navigate Instagram after login
    • βœ… Handling pop-ups (like "Save Info" and "Turn on Notifications")
    • βœ… Clicking profile elements and navigating to different sections

    Let's dive right in!


    πŸ§‘β€πŸ’» Step 1: Handling Post-Login Pop-Ups

    After logging in, Instagram shows pop-ups like:

    • "Save Your Login Info?"
    • "Turn on Notifications"

    We need to dismiss these pop-ups to continue navigating the website.

    πŸ“š Code to Handle Pop-Ups:

    # Handle "Save Your Login Info?" pop-up
    try:
        save_info_popup = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
        )
        save_info_popup.click()
    except:
        print("Save Info pop-up not found.")
    
    # Handle "Turn on Notifications" pop-up
    try:
        notification_popup = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
        )
        notification_popup.click()
    except:
        print("Notification pop-up not found.")

    πŸ§‘β€πŸ’» Step 2: Navigating to the Profile Section

    Once logged in, you can navigate to the profile section to view user information.

    πŸ“š Code to Navigate to Profile:

    # Click on the profile icon
    profile_icon = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Profile']"))
    )
    profile_icon.click()

    πŸ§‘β€πŸ’» Step 3: Liking a Post

    Let's navigate to a post and like it.

    πŸ“š Code to Like a Post:

    # Navigate to the first post
    first_post = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "(//div[@role='button'])[1]"))
    )
    first_post.click()
    
    # Like the post
    like_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Like']"))
    )
    like_button.click()

    πŸ§‘β€πŸ’» Step 4: Posting a Comment

    Next, we'll learn how to post a comment on a post.

    πŸ“š Code to Post a Comment:

    # Click on the comment field
    comment_field = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//textarea[@aria-label='Add a comment…']"))
    )
    comment_field.click()
    comment_field.send_keys("Nice post! 😊")
    
    # Click the post button
    post_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Post')]"))
    )
    post_button.click()

    🎯 Full Code for Today's Lesson:

    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
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--disable-notifications")
    chrome_options.add_argument("--start-maximized")
    
    # Initialize the driver
    service = Service("chromedriver")
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    # Open Instagram
    driver.get("https://www.instagram.com/")
    
    # Explicit wait for the username field
    wait = WebDriverWait(driver, 10)
    username_field = wait.until(EC.presence_of_element_located((By.NAME, "username")))
    password_field = wait.until(EC.presence_of_element_located((By.NAME, "password")))
    login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
    
    # Enter credentials
    username_field.send_keys("your_dummy_username")
    password_field.send_keys("your_dummy_password")
    login_button.click()
    
    # Handle "Save Your Login Info?" pop-up
    try:
        save_info_popup = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
        )
        save_info_popup.click()
    except:
        print("Save Info pop-up not found.")
    
    # Handle "Turn on Notifications" pop-up
    try:
        notification_popup = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
        )
        notification_popup.click()
    except:
        print("Notification pop-up not found.")
    
    # Navigate to the profile section
    profile_icon = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Profile']"))
    )
    profile_icon.click()
    
    # Like the first post
    first_post = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "(//div[@role='button'])[1]"))
    )
    first_post.click()
    
    like_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Like']"))
    )
    like_button.click()
    
    # Post a comment
    comment_field = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//textarea[@aria-label='Add a comment…']"))
    )
    comment_field.click()
    comment_field.send_keys("Nice post! 😊")
    
    post_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Post')]"))
    )
    post_button.click()
    
    # Close the browser
    driver.quit()

    🎯 Task for Today:

    • 1️⃣ Handle Instagram's pop-ups dynamically.
    • 2️⃣ Navigate to your profile and like the first post.
    • 3️⃣ Post a comment on a post.

    Would you like me to explain how to scrape Instagram data next? 😊