Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Scroll Down Followers Popup on Instagram using Python
Instagram is a popular social media platform that allows users to connect and share content with their followers. As a developer, you might need to automate certain tasks on Instagram, such as extracting follower data. Instagram's follower popup only loads a limited number of followers at a time, requiring users to scroll down to view more followers. In this article, we will explore how to scroll down the followers popup on Instagram using Python and Selenium WebDriver.
Prerequisites
Before starting, you need to install the required dependencies:
pip install selenium
You also need to download ChromeDriver from the official website and set up the path correctly.
Key Methods and Syntax
WebDriver Initialization
driver = webdriver.Chrome('path/to/chromedriver')
This method creates an instance of the Chrome WebDriver. It requires providing the path to the chromedriver executable as a parameter.
Navigation and Element Interaction
driver.get(url) element = driver.find_element(By.NAME, name) element.send_keys(value)
These methods navigate to URLs, locate elements, and simulate keyboard input respectively.
Explicit Waits
wait = WebDriverWait(driver, timeout)
This class sets up explicit waits in Selenium, taking the WebDriver instance and maximum wait time as parameters.
Step?by?Step Implementation
Step 1: Import Required Libraries
First, import all necessary libraries for web automation ?
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time
Step 2: Initialize WebDriver
Create an instance of ChromeDriver with the path to the executable ?
driver = webdriver.Chrome('path/to/chromedriver')
Step 3: Navigate and Login
Navigate to Instagram's login page and locate the login fields ?
# Navigate to Instagram's login page
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
# Find and fill login credentials
username_input = driver.find_element(By.NAME, 'username')
password_input = driver.find_element(By.NAME, 'password')
username_input.send_keys('your_username')
password_input.send_keys('your_password')
password_input.submit()
Step 4: Navigate to Profile
After successful login, navigate to the target user's profile ?
# Wait for login to complete
time.sleep(5)
# Navigate to user's profile
driver.get('https://www.instagram.com/target_username')
time.sleep(2)
Step 5: Open Followers Popup
Locate and click on the followers button to open the popup ?
# Find and click followers button
followers_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[contains(@href, "/followers/")]'))
)
followers_button.click()
Step 6: Scroll Through Followers
Wait for the popup to load and implement the scrolling mechanism ?
# Wait for followers popup to load
followers_popup = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//div[contains(@class, "isgrP")]'))
)
# Scroll down the followers popup
scroll_script = "arguments[0].scrollTop = arguments[0].scrollHeight;"
while True:
# Count followers before scrolling
last_count = len(driver.find_elements(By.XPATH, '//div[contains(@class, "isgrP")]//li'))
# Execute scroll
driver.execute_script(scroll_script, followers_popup)
time.sleep(2) # Wait for new followers to load
# Count followers after scrolling
new_count = len(driver.find_elements(By.XPATH, '//div[contains(@class, "isgrP")]//li'))
# Break if no new followers loaded
if new_count == last_count:
break
print(f"Total followers loaded: {new_count}")
Step 7: Clean Up
Close the browser and free system resources ?
driver.quit()
Complete Example
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def scroll_instagram_followers(username, password, target_user):
# Initialize driver
driver = webdriver.Chrome('path/to/chromedriver')
try:
# Login to Instagram
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
username_input = driver.find_element(By.NAME, 'username')
password_input = driver.find_element(By.NAME, 'password')
username_input.send_keys(username)
password_input.send_keys(password)
password_input.submit()
time.sleep(5)
# Navigate to target profile
driver.get(f'https://www.instagram.com/{target_user}')
time.sleep(2)
# Click followers button
followers_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[contains(@href, "/followers/")]'))
)
followers_button.click()
# Wait for popup and scroll
followers_popup = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//div[contains(@class, "isgrP")]'))
)
scroll_script = "arguments[0].scrollTop = arguments[0].scrollHeight;"
while True:
last_count = len(driver.find_elements(By.XPATH, '//div[contains(@class, "isgrP")]//li'))
driver.execute_script(scroll_script, followers_popup)
time.sleep(2)
new_count = len(driver.find_elements(By.XPATH, '//div[contains(@class, "isgrP")]//li'))
if new_count == last_count:
break
print(f"Successfully loaded {new_count} followers")
finally:
driver.quit()
# Usage
scroll_instagram_followers('your_username', 'your_password', 'target_username')
Important Considerations
Rate Limits: Instagram has rate limits and anti?bot measures. Use delays between actions to avoid detection.
Terms of Service: Ensure your usage complies with Instagram's Terms of Service and API guidelines.
Error Handling: Implement proper error handling for network issues, element not found exceptions, and login failures.
Conclusion
Using Selenium WebDriver, you can automate scrolling through Instagram's followers popup to load all followers. The key is to use JavaScript execution for scrolling and implement a loop that continues until no new followers are loaded. Always respect Instagram's terms of service and implement appropriate delays to avoid detection.
