As we all know repeating tasks are boring. On a normal day after the login to my computer, I will open the browser and go to websites like GitHub, Evernote, my blogs, etc. To avoid this repeating and boring task, I made an application to do the same for me every day.
For automatic login to any website we can use Selenium WebDriver and Python.
Selenium WebDriver is a browser-controlling library, it supports all major browsers and is available for different programming languages including Python. In this, we will use the Python bindings for Selenium WebDriver.
Step 1 : Install Selenium and the drivers
We need to install Selenium first, run the below command for installing it.
pip install -U selenium
The next step is install the driver specific for our browser. I am using Firefox and requires geckodriver, you can choose your own. After downloading the driver, unzip it and put it in your application root folder.
Step 2 : Create Pythom script and initialize WebDriver
After that, open a new Python script and initialize the WebDriver with below code.
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait #Site Credinitials username = 'firstname.lastname@example.org' password = 'pass' #Initialize Firefox driver driver = webdriver.Firefox()
Step 3 : Input login credentials
Since we are going to make an app which is automating the GitHub login and we need to inspect the login page as shown below.
From the developer tools, we can see that the username/email input filed has login_field id, where the password input field has the ‘password’ id, also the name of the submit button is
‘commit’. The below code will extract these elements, fill in the credentials, and clicks the button.
# go to github login page driver.get("https://github.com/login") # find username field and send the username itself to the input field driver.find_element_by_id("login_field").send_keys(username) # find password input field and insert password also driver.find_element_by_id("password").send_keys(password) # click the login button driver.find_element_by_name("commit").click()
The find_element_by_id() will extract the HTML element id and the send_keys() method stimulates keypresses, so the above code will open the Firefox instance and type the username and password, and then click the submit button by using the click() method.
Step 4 : Validating credentials and login to the website
After inputting the credentials, we need to find out whether our login is successful or not, for a simple method, we will look at some error message upon the login. Of course, the id and the error messages will be different for every website.
As the above image shows we can see a new HTML div element with the class
'flash-error' that has the text of ‘Incorrect username or password.’.
The below code is responsible for waiting for the page to be loaded with WebDriverWait(), and checks for the error:
# wait the ready state to be complete WebDriverWait(driver=driver, timeout=10).until( lambda x: x.execute_script("return document.readyState === 'complete'") ) website_error_message = 'Incorrect username or password.' # get the errors (if there are) errors = driver.find_elements_by_class_name("flash-error") # if we find that error message within errors, then login is failed if any(website_error_message in e.text for e in errors): print("Error : Login failed") else: print("Success : Login successful")
We use WebDriverWait to wait until the document is finished loading, the
return document.readyState.complete' returns
True when the page is loaded, and
After that we can close the driver:
# close the driver driver.close()
With the above code now we can automatically to the website of your choice. Note that the login process will differ from one website to another, but the above will give a guideline to automate the login process of your target website.