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, there might be a 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.

Syntax

webdriver.Chrome('path/to/chromedriver

Here, this method is used to create an instance of the Chrome WebDriver. It requires providing the path to the chromedriver executable as a parameter.

driver.get(url)

Here, This method is used to navigate to the specified URL in the web browser. It takes the URL as a parameter and loads the corresponding web page.

element = driver.find_element_by_name(name)

Here, This method is used to locate an HTML element on the web page by its name attribute. It returns a WebElement object representing the found element

element.send_keys(*value)

Here, This method is used to simulate keyboard input into an input field or element. It takes the input value as a parameter and enters it into the specified element.

wait = WebDriverWait(driver, timeout)

Here, this class is used to set up explicit waits in Selenium. It takes the WebDriver instance and the maximum amount of time to wait as parameters.

Step−by−Step Implementation

Step 1: Importing and Setting up the environment: We need to first install the required dependencies including Python, selenium, and the appropriate webdriver, and import these libraries in your Python script. The libraries can be installed using Python package manager pip. You can import the required libraries as follows :

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: Initializing the WebDriver: To initialize the web driver you need to create an instance of webdriver.Chrome and provide the path to the Chrome driver executable. You can initalize the ChromeDriver as follows :

from selenium import webdriver

driver = webdriver.Chrome('path/to/chromedriver')

Step 3: Navigate to Instagram's Login Page: By using the driver.get() method you can navigate to the Instagram login page. After navigating to the page locate the username and password input fields using Selenium’s find_element_by_name method.

# Navigate to Instagram's login page
driver.get('https://www.instagram.com/accounts/login/')

# Wait for the page to load
time.sleep(2)

# Find the username and password input fields, and fill in the login credentials
username_input = driver.find_element_by_name('username')
password_input = driver.find_element_by_name('password')

Step 4: Filling in Login Credentials: When you have located the username and password input field using the find_element_by_name() method, you can pass the login credentials to the input field using the send_keys() method on the respective input field.

username_input.send_keys('your_username')  # Replace 'your_username' with your Instagram username
password_input.send_keys('your_password')  # Replace 'your_password' with your Instagram password

Step 5: Submitting the Login Form: After passing the credentials you can now submit the login form by calling the submit() method on the password input field.

# Submit the login form
password_input.submit()

Step 6: Navigating to the User's Profile Page: After successful login, you can use the get() method of the WebDriver to navigate to the user's profile page. Replace 'your_username' with your Instagram username. You can also use the time.sleep() method to wait for the page to load completely.

# Wait for the login process to complete
time.sleep(5)

# Navigate to the user's profile page
driver.get('https://www.instagram.com/your_username')  # Replace 'your_username' with your Instagram username

# Wait for the profile page to load
time.sleep(2)

Step 7: Clicking on the Followers Button:You can find the followers button on the profile page using the find_element_by_xpath() method and an XPath selector. The WebDriverWait class is used to wait until the element is present on the page. Then click on the followers' button using the click() method.

# Find and click on the followers button
followers_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//a[@href="/your_username/followers/"]'))
)  # Replace 'your_username' with your Instagram username
followers_button.click()

Step 8: Waiting for the Followers Popup to Load: We use WebDriverWait to wait until the followers popup is present on the page. Then locate the followers popup using the find_element_by_xpath() method and an XPath selector.

# Wait for the followers popup to load
followers_popup = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//div[@class="isgrP"]'))
)

Step 9: Scrolling Down the Followers Popup:Define a JavaScript script to scroll the followers popup to the bottom. The script sets the scrollTop property of the element to its scrollHeight, which scrolls it to the maximum height. We use a while loop to repeatedly execute the scrolling script until no new followers are loaded. Inside the loop, we keep track of the number of followers before and after scrolling. If the number of followers remains the same, it means no new followers were loaded, and we break out of the loop.

# Scroll down the followers popup
scroll_script = "arguments[0].scrollTop = arguments[0].scrollHeight;"
while True:
    last_count = len(driver.find_elements_by_xpath('//div[@class="isgrP"]//li'))
    driver.execute_script(scroll_script, followers_popup)
    time.sleep(1)  # Add a delay to allow time for the followers to load
    new_count = len(driver.find_elements_by_xpath('//div[@class="isgrP"]//li'))
    if new_count == last_count:
        break  # Exit the loop if no new followers are loaded

Step 10: Closing the Web Browser:

Call the quit() method on the WebDriver to close the web browser and free system resources.

Output

Conclusion

In this article we discussed how we can scroll down the followers' popup on Instagram using Selenium in Python. By leveraging Python and Selenium WebDriver, we can scroll down the followers' popups on Instagram and extract follower data efficiently. In this article, we discussed the step−by−step process and provided example code snippets to help you get started.

Updated on: 18-Jul-2023

617 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements