Automate Instagram Login Using Python Selenium

ยท

2 min read

Table of contents

No heading

No headings in the article.

Today, We will log in to Instagram using the Selenium to do this task.

Package needed:

  • Selenium package
# for Windows
pip install selenium

# for Linux/Max
pip3 install selenium
# or
sudo -H pip3 install selenium
  • Chromedriver compatible with the existing chrome version download

imports:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

constants:

URL = "https://www.instagram.com/accounts/login/"
EXE_PATH = r"C:\Users\vinayak\Documents\chromedriver_win32\chromedriver"

ChromeDriver class:


class ChromeDriver:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome(executable_path=EXE_PATH)
        self.driver.implicitly_wait(10)

    def login(self):
        self.driver.get(URL)
        self.driver.maximize_window()
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']/div//input[@name='username']"
        ).send_keys(self.username)
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']/div/div[2]/div//input[@name='password']"
        ).send_keys(self.password)
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']//button[@type='submit']"
        ).click()
        # to clean popups after login
        self.afterLogin()


    def afterLogin(self):
        try:
            self.driver.find_element_by_xpath(
                "//button[contains(text(),'Save Info')]"
            ).click()
        except NoSuchElementException:
            print("no save Info")
        try:
            self.driver.find_element_by_xpath(
                "//*[contains(@class, 'aOOlW   HoLwm ')]"
            ).click()
        except NoSuchElementException:
            print("no notification box")
        time.sleep(100)

Here, we have two functions:

  • login: this function opens Instagram and login to Instagram

  • afterlogin: after logging Instagram shows some popups, this function removes them

Testing code:

def main():
    username =  input("enter username: ")
    password =  input("enter password: ")
    test = ChromeDriver(username, password)
    test.login()


if __name__ == "__main__":
    main()

Full Code:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

# constants
URL = "https://www.instagram.com/accounts/login/"
EXE_PATH = r"C:\Users\vinayak\Documents\chromedriver_win32\chromedriver"


class ChromeDriver:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome(executable_path=EXE_PATH)
        self.driver.implicitly_wait(10)

    def login(self):
        self.driver.get(URL)
        self.driver.maximize_window()
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']/div//input[@name='username']"
        ).send_keys(self.username)
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']/div/div[2]/div//input[@name='password']"
        ).send_keys(self.password)
        self.driver.find_element_by_xpath(
            "/html//form[@id='loginForm']//button[@type='submit']"
        ).click()
        # to clean popups after login
        self.afterLogin()


    def afterLogin(self):
        try:
            self.driver.find_element_by_xpath(
                "//button[contains(text(),'Save Info')]"
            ).click()
        except NoSuchElementException:
            print("no save Info")
        try:
            self.driver.find_element_by_xpath(
                "//*[contains(@class, 'aOOlW   HoLwm ')]"
            ).click()
        except NoSuchElementException:
            print("no notification box")
        time.sleep(100)


def main():
    username =  input("enter username:  ")
    password =  input("enter password:   ")
    test = ChromeDriver(username, password)
    test.login()


if __name__ == "__main__":
    main()

Did you find this article valuable?

Support Vinayak Sharma by becoming a sponsor. Any amount is appreciated!

ย