Skip to content
Related Articles

Related Articles

Like instagram pictures using Selenium | Python
  • Difficulty Level : Easy
  • Last Updated : 30 Oct, 2020

In this article, we will learn how can we like all the pictures of a profile on Instagram without scrolling and manually clicking the buttons. We will be using Selenium to do this task. 

Packages/Software needed:

1. Python 3 
2. Chromedriver compatible with the existing chrome version (download chromedriver) 
3. Google chrome 
4. Selenium package (pip install selenium), bs4 package(pip install bs4)

Step #1: Importing modules and entering the login information along with the URL of the page. 


from bs4 import BeautifulSoup as bs
import selenium.common.exceptions
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
print("enter username")
username = input()
print("enter password")
password = input()
print("enter the url")
url = input()

Step #2: Function to enter the path where the chromedriver.exe file exists in your system 


def path(): 
    global chrome
    print("enter the driver path")
    exe_path = input()
    # starts a new chrome session
    chrome = webdriver.Chrome(executable_path = exe_path)

Step #3: Function to enter the URL of the page 


def url_name(url): 
    # the web page opens up
    # webdriver will wait for 4 sec before throwing a 
    # NoSuchElement exception so that the element
    # is detected and not skipped.

Step #4: Function to enter your login information 


def login(username, your_password):
    # finds the login button
    log_but = chrome.find_element_by_class_name("L3NKy")
    # clicks the login button   
    # finds the username box
    usern = chrome.find_element_by_name("username")   
    # sends the entered username
    # finds the password box
    passw = chrome.find_element_by_name("password")   
    # sends the entered password
    notn = chrome.find_element_by_class_name("yWX7d")# dont save info button click don't save button

Step #5: Function to open the first picture 


def first_picture():
    # finds the first picture
    pic = chrome.find_element_by_class_name("kIKUG")   # clicks on the first picture

Step #6: Function to like a picture 


def like_pic():
    like = chrome.find_element_by_class_name('fr66n')
    soup = bs(like.get_attribute('innerHTML'),'html.parser')
      if(soup.find('svg')['aria-label'] == 'Like'):

Step #7: Function to click on the Next button 


def next_picture():
        nex = chrome.find_element_by_class_name("coreSpriteRightPaginationArrow")
        return nex
    except selenium.common.exceptions.NoSuchElementException:
        return 0

Step #8: Function which continues liking pictures till it is not able to find the next button 


def continue_liking():
        next_el = next_picture()
        # if next button is there then
        if next_el != False:
            # click the next button
            # like the picture
            print("not found")

Step #9: Calling the functions 


login(username, password)

There you go! The script will automatically like all the posts until the end of the page.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :