LogIn Instagram using Selenium

Introduction

Here is the definition of Selenium given by their official website:

Selenium automates browsers.

What are the functionalities we want to implement for our bot?

The goal of this article is to give you an overview of the possibilities given by Selenium, so I won’t be able to show you how to code every action possible by our bot on Instagram, but with the knowledge you’ll acquire reading this article, you will be able to add the missing functionalities on your own. For now, our bot should be capable of the following action

-Open Instagram.
-Sign in.

Script

from selenium import webdriver     
# For using sleep function because selenium 
# works only when the all the elemets of the 
# page is loaded.
import time
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe") 
  
url = "https://www.instagram.com/"
username ="305kishan" #Enter your id
password="test"  #Enter your password
browser.get(url) #this will open Instagram homepage in chrome 
time.sleep(3)
  
browser.find_element_by_xpath("//a[@class='_l8p4s'][2]").click()
time.sleep(3)
  
a = browser.find_element_by_xpath("//div[@class='_t296e'][1]/div[1]/input[1]")
a.send_keys(username) #For entering username
  
b = browser.find_element_by_xpath("//div[@class='_t296e'][2]/div[1]/input[1]")
b.send_keys(password) # For entering password

For more on how to locate html elements visit the selenium documentation.

How useful this article was

Rating: 5 out of 5.

Published by 305kishan

Aspiring Data Scientist, Hardcore Gamer and Die Hard fan of Cristiano Ronaldo.

Leave a comment

Design a site like this with WordPress.com
Get started