• Home
  • Tutorials
    • Python
    • Machine Learning
    • Pygame
    • Python Quiz
  • 100+ Projects
    • 100+ Python Projects
    • 100+ Java Projects
  • PYTHON HANDWRITTEN NOTES
    • Python Short Notes
    • Complete DSA Notes
    • Python Detailed Notes
  • Python Compiler
    • All Editors
    • Turtle Compiler
    • Online Java Compiler
    • Online C++ Compiler
    • Online C Compiler
  • Contact Us
    • About Us
    • Policy
  • Our Android Apps
    • Unlimited Python Exercises App
    • Complete Linux Commands App

CopyAssignment

We are Python language experts, a community to solve Python problems, we are a 1.2 Million community on Instagram, now here to help with our blogs.

×

Automate Instagram Login Using Python Selenium

 Ankur Gajurel  December 18, 2020
Automate Instagram Login Using Python Selenium

Python is a vast and diverged topic in today’s world, and web automation is one of them. In this article, we will be learning how we can Automate Instagram Login Using Python Selenium.

Selenium is a tool that helps us to automate web browsers. Selenium is used for testing web applications. However, it can do much more stuff. It opens up the web browser and does things a normal human being would do. Some of those things include clicking buttons, searching for information, filling the input field even though it is a handy tool. If you scrape a website frequently and for malicious purposes, your IP address may be banned as it is against most of the websites’ terms and services.

Moreover, We already have a full Selenium tutorial of selenium from basics to advance. You can also see that tutorial. In this tutorial, I will explain each part of the python code to understand all the basics of selenium.


There are two requirements–

1. Installing the Selenium Module.

pip install selenium

You can install selenium from Git Bash like in the following picture.



2. Installing the Selenium Web Driver

Now, after you have the selenium “module” for “Automate Instagram Login Using Python Selenium”, you will need a web driver.

ChromeDriver or any other browser’s driver like firefox also needs to be downloaded by us because when we want to automate something with selenium, these drivers are necessary, without them, we can not open any browser, and if we can not open a browser then we can not automate anything, Right!

Chrome Browser Version

Before you download the chromedriver, keep in mind that the version of the chromedriver you are downloading should match with the version of the Chrome Browser which is installed in your system. You can know the version of your browser from the following steps.

  1. Click on the three dots on the top right corner of your browser.
  2. Then, see through the given options and click on the help button
  3. Likewise, click on the “About Chrome” or any other browser you are using.
  4. This will open an interface that will show you about the information of your browser. See through the page and you will find the version of your browser like given in the picture below.

You can download the webdrivers from the table below.

ChromeChrome Driver
EdgeEdge Driver
FirefoxGecko Driver
OperaOpera Driver
SafariSafari WedDriver is built-in

Likewise, after you have both the module and the web driver, you can start coding.


Code for Automate Instagram Login Using Python:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

name = input("Enter your username: ")
passW = input("Enter your password: ")

driver = webdriver.Chrome(executable_path="C://Users//Downloads//chromedriver.exe")

class InstaBot():
	def __init__(self, userName, passWord):
		self.userName = userName
		self.passWord = passWord

	def login(self):
		driver.get("https://www.instagram.com")

		elemUser = driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input')
		elemUser.send_keys(self.userName)

		elemPass = driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input')
		elemPass.send_keys(self.passWord)


ankur = InstaBot(name, passW)
ankur.login()

Explanation:


First Part:

  • First, import all the module required that is webdriver from selenium and keys from selenium webdriver common keys and the time.
  • After you have imported all the required modules, you can ask the user to input their username and password which will be stored in the variables “name” and “passW”. Likewise, you can specify where the driver is with the code below.
driver = webdriver.Chrome(executable_path="PATH TO CHROMEDRIVER")

The class InstaBot:

  • Accordingly, create a class called InstaBot. Then, inside the class create a initializer with the parameters of userName and passWord. Always keep in mind that you should keep “self” parameter in all the functions inside of a class. Similarly, inside the initializer, set the value of self.userName to userName and the value of self.passWord to passWord.
  • Coming out of the initializer, create a function named login() and don’t forget to keep the parameter “self”. Accordingly, inside the login() function, browse to the instagram login page by the get method.

Now, we need to enter the login credentials i.e. username and password Right! and after that we need to click on the “Log In” button. Also, we want all this to be done automatically.

Here comes the basic HTML part in role i.e. if you know the basics of HTML then it will be a benefit for you to Automate Instagram login using Python.

If not, let me do this for you, selenium uses HTML elements to define blocks of different types like paragraph, headings, horizontal line, and many more like this. Likewise, to know more detailed information about HTML elements, click here.



HTML Attributes

Every HTML elements have some attributes. An attribute is used to define the characteristics of an HTML element and is placed inside the element’s opening tag.

We use HTML attributes to target the elements for example, to enter email-id inside “Username” block on the Instagram login page, we need it one of the available attributes like name, id, class, etc.

If we get the attribute of that element, we can pass values to that element accordingly and if the element is a button, then also we can use its attribute to click on that button.

Locating Methods In Python Selenium

Moreover, there are many ways to locate elements on a web page in Python Web scraping using Python Selenium. You can use it according to the environment you want. The methods to locate elements on a web page are:

  1. find_element_by_id
  2. find_element_by_name
  3. find_element_by_xpath
  4. find_element_by_link_text
  5. find_element_by_partial_link_text
  6. find_element_by_tag_name
  7. find_element_by_class_name
  8. find_element_by_css_selector

If you want to find many elements on a page then you can use the following methods.

  1. find_elements_by_name
  2. find_elements_by_xpath
  3. find_elements_by_link_text
  4. find_elements_by_partial_link_text
  5. find_elements_by_tag_name
  6. find_elements_by_class_name
  7. find_elements_by_css_selector

Note: The above methods will return a list.

You can follow the below steps to copy the xpath or any other attributes you want.

  1. Browse to the web page you want and right click on your mouse. This will show many options but click on the “Inspect” option. You can also do this with a keyboard shortcut Ctrl + Shift + I. This will bring up a bunch of html code which is the exact code of the web page.
  2. Now, click on the mouse icon on the top left of the new window.
  3. Then, click on the input or any element you want on the web page. This will locate you to the code of the same element.
  4. Likewise, right click on the code of the element that you were located to.
  5. Now, this will show many options but click on “Copy” on the end of the page.
  6. And click on “Copy full XPath option”. This will copy the xpath of the element into your clipboard.
  • After the above steps, locate the input element of username and store in “elemUser”. Do the same with password and store in “elemPass”. Likewise, send keys into the username element and password element with the code above.

Last Part, Objects and calling login function:

  • Lastly, name the “ankur” variable as the object to InstaBot and send the arguments “name” i.e username and “passW” i.e password.

Comment your queries or anything you want to know or tell us based on this post or website.

Thanks for reading

Keep Coding, Keep Learning


Also Read:


Python Web scraping using Python Selenium

Whatsapp Spam Bot Using Python Selenium

Automate Facebook Login Using Python Selenium

Python Send Email Program

Shutdown Computer with Voice in Python

Jarvis and Google Assistant || Voice Assistant Using Python


Share:
Avatar of Ankur Gajurel

Author: Ankur Gajurel

I am Ankur from Nepal trying to learn different aspects of Information Technology and Physics. I like building websites and minor projects with Python.
Github

Post navigation

← Python Turtle Design of Spiral Square and Hexagon
Python Turtle Design in Indian Flag Color →

Categories

30 Days of Code allcategorites assignment c and cpp competitive programming data structures and algorithms django tutorial final year project game in python general-python gfg github-copy gui java project gui python projects how-to html css javascript project java java game projects java projects job leetcode Machine Learning ml-shorts ml ai ds project php projects programs in python project ideas pygame tutorial python-error python-short-tutorial python projects python simple programs python sqlite qna python turtle python very small program assignments tutorial web projects

Python Automation Projects

Selenium webdriver download

Download Chrome Driver for Selenium

Python Web scraping using Python Selenium

Automate Facebook Login Using Python Selenium

Automate Instagram Login Using Python Selenium

Whatsapp Spam Bot Using Python Selenium

Jarvis || Voice Assistant Using Python

Shutdown Computer with Voice in Python

Sitemap

Python

Machine Learning

Pygame

Data Structures and Algorithms(Python)

Python Turtle

Games with Python

All Blogs On-Site

Python Compiler(Interpreter)

Online Java Editor

Online C++ Editor

Online C Editor

All Editors

Services(Freelancing)

Recent Posts

  • Top 25 Pattern Programs in C++
  • Currency Converter in C++
  • SQLite | CRUD Operations in Python
  • Number Guessing Game in C++
  • Image background remover in Python

Must Read

  • 100+ Python Projects
  • 100+ Java Projects
  • Python Programs
  • JavaScript Projects
  • Java Projects
  • PHP Projects

RoadMap to Learn

  • Python
  • Java
  • JavaScript

Python Handwritten Notes

Click Here to buy now

Python Assignment Help

We also deal with Python Assignments, Homeworks, and Projects. Click Here, to get your Python work done by experts.

close button

© Copyright 2022 www.copyassignment.com. All rights reserved. Developed by copyassignment