You are currently viewing Instagram automatically login, scrolling up/down and log out using Python Selenium
Instagram Automatically login using Selenium Python

Instagram automatically login, scrolling up/down and log out using Python Selenium

In this blog, I will share you the source code of Instagram automatically login using Python and Selenium. You can copy and paste this code in you favorite code editor.

1)config.py

username = "abc"    # Replace your Instagram username
password = "abc@11" # Replace your Instagram password

2)instagram_login.py

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
from cofig import username, password
import time

text_to_check = "Not now"

# Create a new instance of the browser
driver = webdriver.Chrome()
driver.maximize_window()

try:
    driver.get("https://www.instagram.com/")

    wait = WebDriverWait(driver, 10)

    u_name = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='username']")))
    u_name.send_keys(username)

    p_word = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='password']")))
    p_word.send_keys(password)

    button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
    button.click()
    
    # Use WebDriverWait to wait for the presence of the text on the webpage
    not_now = wait.until(EC.presence_of_element_located((By.XPATH, f'//*[contains(text(), "{text_to_check}")]')))

    print(f'The text "{text_to_check}" is present on the webpage.')
    
    not_now.click()

    # Wait for the dialog to be present
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@role='dialog'])[2]")))

    # Click on the button within the dialog
    button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Not Now']")))
    button.click()

    print(f'The button within the dialog has been clicked.')
    
    # Scroll down
    driver.execute_script("window.scrollBy(0, 1000);")  # You can adjust the value (500) to scroll by a different amount
    time.sleep(2)  # Optional: Add a short sleep to wait for the scroll to take effect

    # Scroll back up
    driver.execute_script("window.scrollBy(0, -500);")  # You can adjust the value (-500) to scroll back up by a different amount
    time.sleep(2)  # Optional: Add a short sleep to wait for the scroll to take effect

    print('Scrolled down and then scrolled back up.')

    #now we will log out 
    more_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='More']")))
    more_button.click()

    #log out button
    logout_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Log out']")))
    logout_button.click()


except TimeoutError:
    print(f'The text "{text_to_check}" or the dialog is not present on the webpage within the specified timeout.')

finally:
    # Close the browser window
    time.sleep(5)
    driver.quit()

3)requirements.txt

selenium==4.14.0  #you can install latest version of selenium

4)README.md

# Project Name

Instagram automatically login, scrolling up/down and log out using Python Selenium.

## Prerequisites

- [Python](https://www.python.org/downloads/) (version X.X recommended)
- [Your favorite IDE](#) (e.g., [PyCharm](https://www.jetbrains.com/pycharm/download/), [Visual Studio Code](https://code.visualstudio.com/download/))

## Getting Started

### Installation

1. **Install Python**: Download and install Python from [here](https://www.python.org/downloads/).
   
2. **Install your favorite IDE**: Download and install your preferred IDE. Some popular options include:
   - [PyCharm](https://www.jetbrains.com/pycharm/download/)
   - [Visual Studio Code](https://code.visualstudio.com/download/)
   - [Jupyter Notebook](https://jupyter.org/install.html)

### Setup Project

1. **Create a new project in your IDE**: Open your IDE and create a new project.

2. **Create and activate a virtual environment**:
   - **For Linux/Mac**:
     ```
     python -m venv myenv       # Create a virtual environment named 'myenv'
     source myenv/bin/activate  # Activate the virtual environment
     ```
   - **For Windows**:
     ```
     python -m venv myenv       # Create a virtual environment named 'myenv'
     myenv\Scripts\activate     # Activate the virtual environment
     ```

3. **Install all requirements**:
   pip install -r requirements.txt

Leave a Reply