Automating Instagram login using Python | TheCodingNeuron
In this tutorial, we will be learning how we can automate Instagram login using Python. We will be using the Selenium.py module to perform this task.
At the end of this tutorial, you will be able to automate any login process. Let's get started with this tutorial
Index :
- What is selenium?
- Installing Chromedriver and selenium
- Automating Instagram Login
What is Selenium?
Selenium is basically an open-source package used for browser automation, Its main purpose is to test if a software is working in a way that it should be working or not. But now it is also used for other repetitive automation tasks. Selenium driver is available in many languages like JavaScript (Node. js), Python, Ruby, Java, Kotlin (programming language), and C#.
For example,
Selenium driver can be used for simple tasks like automating my homework to complex automation scripts that scrape huge amounts of data.
Well, don’t worry this tutorial is beginner-friendly as well as important because I am going to explain things in detail.
Installing Chromedriver and selenium
Before logging into Instagram we need to open chrome and for that, we need to install a chrome driver that will be used to drive or perform actions on chrome.
Here is the catch, when installing chrome-driver make sure that you install the correct version of chrome-driver that matches your chrome browser version
To check your chrome version you can search this in chrome
chrome://version
once you know the version, visit https://chromedriver.chromium.org/downloads
to download chromedriver
Click on the version that matches your chrome browser and download it (it's an important step so make sure the chrome driver is compatible)
the next step is to install Selenium.py.
Assuming that you have python already installed on your PC. If not you can download it from here https://www.python.org/downloads/.
Moving forward open the Command Prompt from the Start menu and type in
pip install selenium
Once Selenium is Installed its time to write some Python code Let's jump into our code editor
First of all, create a “main.py” file and place the chromedriver.exe that you downloaded in the same directory as the python file like this
let's start by importing selenium into the main.py file
- ) first, we initialized a chrome driver by using webdriver.Chrome().
- ) Next, we used the driver.get() that takes in the URL as an argument. we used “https://instagram.com/” since we want to open Instagram.
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://instagram.com/")
After running this script you will see a chrome window popping up and opening Instagram.
After this, we need to input our credentials like email and password. But before that, we need to tell the selenium driver where to put our credentials.
As we know that every single site we visit has HTML and that HTML has different tags with different ids, classes, names, and values.
Selenium provides us with different functions to search for these tags and input our data or perform actions on them.
For example
- finding elements by id
- finding elements by XPath
- finding elements by Classname etc
First, we need to find the tag and id of the email section so we will right-click on the “Email or username” input box and press inspect. After pressing inspect chrome developer will open in the same tab. where you can check for their respective tags, id, etc.
As you can there are multiple attributes that we can target. In our case, we will “search it by using Name attribute” which is “name=username”
Similarly, we are going to search for password block where “name=password”
Process :
- we created two variables, user and passwords that are our actual credentials.
- next, we opened Instagram
- To find the element selenium provides us the “driver.find_element()” function, which takes in two arguments, first, how you want to search it,i.e tag name, XPath, id, class name, etc. The second is the “value”.
Suppose you want to find and click a button with id=“btn”. We will implement it like this 👇
driver.find_element(by=By.ID, value=“btn”).click() - Once you find an element, we can also write inside the element block by using the send_keys() function. In our case, we use it to write username and password.
- After sending the username and password we need to press the login button. To find and click the button we used the XPath of the button.
- Xpath is the location or Path of an element in the DOM, “document object Model”. You can learn how to create XPath over here. Or use these chrome extensions to generate xpath
Final CODE:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
user = "Enter username"
password = "enter password here"
driver = webdriver.Chrome()
driver.get("https://instagram.com/")
time.sleep(2)
driver.find_element(by=By.NAME ,value='username').send_keys(user)driver.find_element(by=By.NAME,value='password').send_keys(password)driver.find_element(by=By.XPATH, value="//button[1]/div[1]").click()
Result:
Stay tuned for more automation and webdev tutorials.
You can Support me by :
- Giving Claps 👏on this post
- Buy me a Coffee 🍵