Automatic Instagram Login using Python-selenium

Hello Coders!

In these blog I'll show how you can login to your instagram account using Python-Selenium.

In this blog we will see:-


1.  What is Python Selenium

2. How to install Python selenium and dependencies on windows machine

3. How to Login to instagram using python code.

What is Python Selenium:-

Selenium is a powerful open-source tool for controlling web browsers through programs and performing browser automation. Thus used for automating the test cases carried out on web browsers or the web applications. It is functional for all browsers, works on all major OS, In this article we will be using Windows10.

How to install Python selenium and dependencies on windows machine

To install Python selenium your must have python and pip installed first.

So Lets Get started !!!

pip install selenium

PIP is the python package manager, or Libraries which is used to install any module in python.

After this you have to install drivers, Selenium requires a driver to interface with the chosen browser, you can download drivers from following links:-

Chrome: sites.google.com/a/chromium.org/chromedrive..

Edge: developer.microsoft.com/en-us/microsoft-edg..

Firefox: github.com/mozilla/geckodriver/releases

Safari: webkit.org/blog/6900/webdriver-support-in-s..

Now, let's import the selenium and time package we have installed.

from selenium import webdriver
import time

Now we will sepicify the webdriver, here I have used Firefox, and we have made a get request to instagram Url.

def main():
    driver = webdriver.Firefox()
    driver.get("https://www.instagram.com/accounts/login/")

time.sleep is used for the browser to load the full page and then execute another line of code.

driver.find_element_by_name() is used for Locating Single Elements on the browser page with the name tag such as "username", "password" , "email" etc. then we have filled the username and password with our credentials with send_keys(), it is used to send text to any field.

        time.sleep(4)
        driver.find_element_by_name("username").send_keys("<Your Instagram username or mail id>")
        driver.find_element_by_name("password").send_keys("<Your instgaram password>")

driver.find_element_by_xpath() this method returns a list with type of elements specified. XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications.

click() method is used to click on the Log in button.

        driver.find_element_by_xpath('//button[normalize-space()="Log In"]').click()
        time.sleep(5)
        driver.find_element_by_xpath("//button[@class='sqdOP yWX7d    y3zKF     ']").click()
        time.sleep(5)
        driver.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']").click()

if __name__ == "__main__":
    main()

driver.find_element_by_xpath("//button[@class='sqdOP yWX7d y3zKF ']").click() is used when the browser ask to save the credentials and we don't want to save, driver.find_element_by_xpath("//button[@class='aOOlW HoLwm ']").click() this is used to not allow notifications.

ezgif.com-video-to-gif.gif

Connect with me

Instagram techworld_security

Github repository for code Python-selenium

If you find it useful then please give it a star.

Happy coding !!!