Like instagram pictures using Selenium | Python

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/Softwares needed –

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



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

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
import time
  
print("enter username")
username = input()
  
print("enter password")
password = input()
  
print("enter the url")
url = input()

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Step #4: Function to enter your login information

filter_none

edit
close

play_arrow

link
brightness_4
code

def login(username, your_password):
      
    # finds the login button
    log_but = chrome.find_element_by_class_name("L3NKy")
    time.sleep(2)
  
    # clicks the login button
    log_but.click()    
    time.sleep(4)
  
    # finds the username box
    usern = chrome.find_element_by_name("username")    
  
    # sends the entered username
    usern.send_keys(username)   
  
    # finds the password box
    passw = chrome.find_element_by_name("password")    
  
    # sends the entered password
    passw.send_keys(your_password)      
  
    # finds the login button
    log_cl = chrome.find_element_by_class_name("L3NKy")    
    log_cl.click()   # clicks the login button
    time.sleep(4)

chevron_right


Step #5: Function to opens the first picture

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Step #6: Function to like a picture

filter_none

edit
close

play_arrow

link
brightness_4
code

def like_pic():
    time.sleep(4)
    like = chrome.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span')
  
    # you can find the like button using class name too
    time.sleep(2)
    like.click()   # clicking the like button

chevron_right


Step #7: Function to click on the next button

filter_none

edit
close

play_arrow

link
brightness_4
code

def next_picture():
    time.sleep(2)
  
    # finds the button which gives the next picture
    nex = chrome.find_element_by_class_name("HBoOv")  
    time.sleep(1)
    return nex

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

def continue_liking():
    while(True):
        next_el = next_picture()
  
        # if next button is there then
        if next_el != False:  
  
            # click the next button
            next_el.click()   
            time.sleep(2)
  
            # like the picture
            like_pic()    
            time.sleep(2)            
        else:
            print("not found"
            break

chevron_right


Step #9: Calling the functions

filter_none

edit
close

play_arrow

link
brightness_4
code

path()
time.sleep(1)
  
url_name(url)
  
login(username, password)
  
first_picture()
like_pic()
  
continue_liking()

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.