Select a Subtopic
📚 Day 6: Interacting with Dynamic Elements
Instagram uses dynamic content that loads as you scroll through the page. To interact with these elements (like posts, comments, or followers), we need to handle:
- ✅ Infinite scrolling
- ✅ JavaScript execution with Selenium
- ✅ Locating elements that load dynamically
🧑🏫 Step 1: Handling Infinite Scrolling
Instagram pages like feeds and hashtag pages use infinite scrolling. This means new posts load dynamically when you scroll down. Here's how to handle it with Selenium:
🔧 Code Example: Infinite Scrolling on Instagram
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/") # Wait for the page to load time.sleep(5) # Log in to Instagram (replace with your credentials) username = driver.find_element(By.NAME, "username") password = driver.find_element(By.NAME, "password") username.send_keys("your_username") password.send_keys("your_password") login_button = driver.find_element(By.XPATH, "//button[@type='submit']") login_button.click() # Wait for login to complete time.sleep(5) # Infinite scrolling function def infinite_scroll(): last_height = driver.execute_script("return document.body.scrollHeight") while True: # Scroll down to the bottom driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(2) # Wait for new content to load # Calculate new scroll height and compare with last height new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break # Exit the loop when no more content is loaded last_height = new_height # Call the infinite scroll function infinite_scroll() # Close the browser driver.quit()
🧩 Understanding the Code
- driver.execute_script(): Executes JavaScript code in the browser.
- window.scrollTo(0, document.body.scrollHeight): Scrolls to the bottom of the page.
- document.body.scrollHeight: Returns the total height of the web page.
🛠️ Practical Task
- 1. Logs into Instagram.
- 2. Navigates to a hashtag page.
- 3. Scrolls through the feed to load at least 20 posts.
🧑💻 Step 2: Interacting with Dynamically Loaded Elements
After scrolling, you can interact with the newly loaded elements. For example, liking posts or following users.
🔧 Code Example: Liking Posts on a Hashtag Page
# Navigate to a hashtag page driver.get("https://www.instagram.com/explore/tags/python/") # Wait for the page to load time.sleep(5) # Infinite scroll infinite_scroll() # Locate posts and like them posts = driver.find_elements(By.XPATH, "//a[contains(@href, '/p/')]") for post in posts[:5]: # Like the first 5 posts driver.get(post.get_attribute("href")) time.sleep(2) try: like_button = driver.find_element(By.XPATH, "//span[@aria-label='Like']") like_button.click() time.sleep(2) except Exception as e: print("Already liked or couldn't find the like button.")
🎯 Your Goal for Day 6:
- 1. Scroll through Instagram feeds automatically.
- 2. Interact with dynamically loaded elements (like posts).
Would you like me to help you further with XPath, CSS Selectors, or optimizing your code? 😊