Automate Login System with Selenium WebDriver: A Python Guide from 2019

Automate Login System with Selenium WebDriver: A Python Guide from 2019

Requirements :

  1. Selenium WebDriver (Link is given below)

  2. Python3

  3. Any Text Editor

Let’s start with the Selenium web driver, what is it?

I won’t bore Y’all with the technical stuff, just keep in mind that this is like a driver that can automate everything on the web.

You can download it via this link-

https://www.seleniumhq.org/download/

[Choose Python]

Or you can download the unofficial ChromeDriver file via this:

https://sites.google.com/a/chromium.org/chromedriver/downloads

How do you choose the correct version?

Go to chrome://settings/help check your chrome version

Now that you have done the basic setup let’s get started.

Keep the ChromeDriver file in the same folder you’ll be creating your program.

Before you code or copy-paste this code, please understand the code.

Now let’s see the code:

from time import sleep
from selenium import webdriver

# This will locate and call your ChromeDriver
browser = webdriver.Chrome('./chromedriver')
# get() function redirects your browser to the link you provide
# here we provide the instagram login site 
browser.get('https://www.instagram.com/accounts/login/?hl=en')
# This gets the element name i.e your username field.
# We’ll learn how to do this later (If you are curious)
username = browser.find_element_by_name('username')
# Now we click that ,virtually ofcourse. username.click()
# send_keys() is like typing words on the element 
username.send_keys('username')
# Same goes for Password Field
password = browser.find_element_by_name('password')
password.click() 
password.send_keys('password')
# click on submit button
username.submit()
sleep(100)
browser.close()

Now run the program in your terminal or use your IDLE (PyCham) using this command :

python3 filename.py

Vola! Your automatic login system is ready!

Now if you want to learn web scrapping (getting a web element from the DOM), follow me:

If you are a web developer you can catch on easily, but for someone who has no idea about web development don’t worry, I’ve got your back!

This part you have to do manually for scraping the web elements from the DOM so that your WebDriver chooses the correct elements. Now I have done it so the code will work regardless, this is just for you to understand so that you can make some more things with this.

Now open the Instagram login site and press F12. You see the good ol’ inspect element editor.

How to go Inspect Element

Then select this:

This is how you can select an element

Then you move your cursor to the username field :

Now see the selected text in the Inspect Element :

check for the ‘name’ attribute in the input class.

Similarly, do this for the password field.

And that’s how you Scrape a web element.

I m grateful, thanks for reading :)

Did you find this article valuable?

Support Rahul's Tech Blog by becoming a sponsor. Any amount is appreciated!