Here we will see how to automate the login form using selenium framework and python.

Selenium Automate the Login:

This example requires the following Softwares to be installed in your machine.

You can follow my previous article about how to install the selenium and kickstart with selenium HelloWorld example to get all prerequisites.

Automate the Login Form:

We are going to automate the Facebook login form using python.
from selenium import webdriver

driver = webdriver.Chrome(executable_path="D:\\softwares\\chromedriver_win\\chromedriver.exe")

Code Walkthrough:

  • importing webdriver from selenium package
  • webdriver.Chrome() function is used to launch the web browser. for this, we have to provide the chromedriver.exe path as an executable_path=”{chromedriverpath}”
  • diver.get("URL") function loads a web page ( in the current browser session.
  • driver.find_element_by_id() is a driver function used to finds an element in the web page and returns the element as WebElement. if the element wasn’t found it raises NoSuchElementException exception.
  • send_keys() is a WebElement function used to send simple key events or form fields. In our case, we are sending Facebook username and passwords to the form fields.
  • click() is a WebElement function is used to perform the click event on the element.
  • Finally closing the driver using the close() function.

How to get dom elements:

On the above example, we used find_element_by_id(id) function, this function needs an exact dom element id to find it. So we have to provide that dom element id to this function to get the element.

Inspect the elements using developer tools (F12).

Python Selenium Automate the Login Form

You can observe the above-highlighted dom elements. We can also use the name and class attributes for selection using find_element_by_name() and find_element_by_class_name() functions respectively, you can find more selectors here,


Finally, run the application so that you could see the browser window popping up and automate the login process from selenium on your behalf 🙂



Happy Learning 🙂