Automate login for any webpage using Python

Original article was published on Artificial Intelligence on Medium

Yes, you read it right. You can log in to any website automatically using Python. There is much automation technique you can do using Python. Have you ever got irritated for logging in to the websites you often visit?. So here comes the automation technique to make automated login. So, you don’t need to enter your username or email and password anymore. Except at the setting up! Let’s just jump on the topic.

Source imageflip

Since it’s just simple automation which doesn’t contain much code and doesn’t require coding knowledge. I’ll explain the code in a much easier way. The requirements are

Chrome driver is nothing but a browser which is chromium project made for automation. You can download chrome driver here. Download the chrome driver version as the same as your Google Chrome. You need to install a python package to handle chrome driver. You can install it using pip command. The command is

pip install selenium

Here for this tutorial, I’m going to use twitter for automated login. Now create a new folder somewhere and open any IDE of your choice and paste the below code.

from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="chromedriver.exe")

Place your chrome driver in the same folder where this python file is. Let’s breakdown the code. At 1st line, from selenium Python package we’re importing web driver to handle the chrome driver browser. At 2nd line time package is imported. The path of the chrome web driver is mentioned in the 3rd line. At next line, we’ve provided the twitter’s login URL which will directly take you to the login page of twitter. Then time.sleep(6) line is used to suspend the execution of the program for a given amount of time. Here, 6 means that the program is going to sleep of 6 seconds and after 6 seconds it’ll resume the following code. Actually sleep function is used because the website needs some time to load that’s the main reason for using sleep function in the program. Next line, driver.find_element_by_xpath(‘//*[@id=”react-root”]/div/div/div[2]/main/div/div/form/div/div[1]/label/div/div[2]/div/input’).send_keys(‘YOUR_USERNAME’) is used to find the username input element in twitter login page. Fill your username or email or phone in place of YOUR_USERNAME. Then in the next line of the code is used to autofill the password field. In place of YOUR_PASSWORD fill your password and that’s it! This will the last time you’ll enter your password for the specified website. The last line is to click the login button on that page. The program will automatically click the login button. Now you can run this program. In the code, I have used find_element_by_xpath() function to find the input element and button element. This XPath will vary for the different websites and for different elements so you have to find the required element’s XPath. This can be done in your chrome browser by hovering the mouse on that element and inspecting that element. after inspecting right click on that element and hover over copy option and select copy XPath, not the Full XPath and paste it in the code.

Copying the XPath of the username field.

The above image depicts on copying the XPath of the username. The same process is done for all elements. By using this thing you can automate login for any website. If getting stuck in some issues please let me know in the response section. I’ll try helping you. Start building your own login automation. Not only login automation you can also do much automation using selenium and chrome driver.