DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

Automate Instagram Login Using Python Selenium

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

Enter fullscreen mode Exit fullscreen mode
  • Chromedriver compatible with the existing chrome version download

imports:

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

Enter fullscreen mode Exit fullscreen mode

constants:

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

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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()

Enter fullscreen mode Exit fullscreen mode

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()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)