- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
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
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 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"]')) )
# Scroll down the followers popup scroll_script = "arguments.scrollTop = arguments.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.
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.
Kickstart Your Career
Get certified by completing the courseGet Started