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.
- Click on the three dots on the top right corner of your browser.
- Then, see through the given options and click on the help button
- Likewise, click on the “About Chrome” or any other browser you are using.
- 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.
Chrome | Chrome Driver |
Edge | Edge Driver |
Firefox | Gecko Driver |
Opera | Opera Driver |
Safari | Safari 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:
- find_element_by_id
- find_element_by_name
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_tag_name
- find_element_by_class_name
- find_element_by_css_selector
If you want to find many elements on a page then you can use the following methods.
- find_elements_by_name
- find_elements_by_xpath
- find_elements_by_link_text
- find_elements_by_partial_link_text
- find_elements_by_tag_name
- find_elements_by_class_name
- 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.
- 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.
- Now, click on the mouse icon on the top left of the new window.
- 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.
- Likewise, right click on the code of the element that you were located to.
- Now, this will show many options but click on “Copy” on the end of the page.
- 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
Shutdown Computer with Voice in Python
Jarvis and Google Assistant || Voice Assistant Using Python