How to Automate Login Using Selenium in Python?

Posted in /  

How to Automate Login Using Selenium in Python?
vinaykhatri

Vinay Khatri
Last updated on December 26, 2024

    Automation with Python is fun. Here, we will tell you how to automate login using Selenium in Python.

    The Python programming language supports many third-party libraries to perform automation tasks. Selenium is one of the most popular third-party functional testing libraries for Python. It is often used for controlling web browsers for automation and testing.

    With Selenium web drivers, we can automate the browser(s) installed on our system and automate several tasks, like login, open pages, and search using the browser, just by executing a Python script.

    The Selenium web drivers are compatible with the 4 popular web-browsers, namely Chrome, Firefox, Edge, and Safari. This means that with a single Python library - Selenium - we can control these web browsers and perform automation.

    In automation, automating logging into a website is a very cool and handy trick.

    So, in this Python tutorial, we will walk you through how to automate login to Facebook and Github using Python with Selenium.

    Prerequisites for Automating Login with Python Selenium

    • Python Selenium Library

    Selenium is not a standard Python library. Thus, you need to install it for your Python environment before using it. Use the following command (via the command prompt or terminal) to install the Selenium library:

    pip install selenium
    • Download Selenium Browser Driver

    As Selenium will automate the login using a web browser, we require the web driver to communicate and automate the process in the web browser. For this tutorial, we will be downloading and using the Chrome web driver because most of the developers use it. If you wish you can also download a different web driver from the list below:

    • Edge web driver : https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
    • Firefox web driver : https://github.com/mozilla/geckodriver/releases
    • Safari web driver : https://webkit.org/blog/6900/webdriver-support-in-safari-10/

    Note: Make sure that you are downloading the correct web driver according to your installed web browser version.

    Our Chrome version is 87.0.4280.88 (Official Build) (64-bit) . That's why we are downloading the second option as shown in the above screenshot i.e. ChromeDriver 87.0.4280.88 . Please check your browser version and download the corresponding WebDriver.

    Note: To check your Chrome version click on the menu button at the top right corner, select the Help and click About Google Chrome.

    After Installing the Web Driver zip file , extract it. After extracting the zip file you will see the driver as chromedriver.exe file. We will be using this driver in our Python script. Now, let's code.

    Automate Facebook Login with Python and Selenium

    We will start coding by importing the important modules:

    #use selenium driver
    from selenium import webdriver
    
    #keys to enter input data into fields 
    from selenium.webdriver.common.keys import Keys
    
    #time module to put some delay in the process
    import time 

    After importing the modules we will initialize the Chrome driver that we have just installed.

    #initialize the driver
    driver = webdriver.Chrome(r"C:\Users\tsmehra\Desktop\code\chromedriver.exe")
    
    #open facebook.com with chrome
    driver.get("http://www.facebook.com")

    As you can see that we have provided the absolute path for the chrome driver chromedriver.exe , this will initialize the Chrome browser from your Python script. With the initialized driver, we can open facebook.com with the driver.get() method. This will open the website login page for us.

    As we know that when we try to log into any website, we have to fill out the HTML login form. Consequently, when it comes to Facebook, we have to fill out the login form i.e. the email and password fields.

    But when we want to automate this process with Selenium, we have to tell it which input fields should it fill. To do this, we first need to grab the input fields for email and password . We will also access the login button because once we fill the fields we need to hit it so we could log in.

    The selenium driver object provides the find_element_by_name() method to access the driver page elements. Hence, we will be using this method to access the email, password, and login elements of the facebook.com page.

    To use this method, we should know the name of the input fields and to know the name we can inspect the page and look for their element names.

    By inspecting the facebook.com login page you can see that the email input has name ="email" , password input has name="pass" and the login button has name="login" as attributes. We will be using these names to access their input field and fill in the credentials.

    How to Automate Login Using Selenium in Python? [Facebook Code]

    #Python automation program for Facebook login

    #My Facebook credentials my_email ="myemail@email.com" my_password= "mypassword1@23" #access facebook login email input email_input_box = driver.find_element_by_name("email") #access facebook login password input password_input_box = driver.find_element_by_name("pass") #access facebook login button login_button = driver.find_element_by_name("login") #clear the placeholders data email_input_box.clear() password_input_box.clear() #fill login credentials email_input_box.send_keys(my_email) time.sleep(2) #2 second time gap between filling email and password password_input_box.send_keys(my_password) time.sleep(2) #2 second time delay #hit the login button login_button.click() # automatically close the driver after 30 seconds time.sleep(30) driver.close()

    Now we are all set. Execute the above program and your login automation will start with the Chrome browser and you will see a similar screen as follows:

    After executing the program, a Chrome window will pop up with a message of Chrome is being controlled by automated test software . This means your automation is working. As in the program, we have put 2 seconds delay between filling in the email and password and hitting the login button so you could witness Selenium in action.

    Automate Github Login with Python and Selenium

    Now, let's code for the automated Github login with Python and Selenium. The code, however, will remain pretty much similar to the above example. Thus, let's start by inspecting the Github login page and checking the input field name for the login inputs.

    By inspecting the github.com/login page we found out the names for Username, Password, and Sign in elements. Username or Email Field: name ="login" Password Field: name ="password" Sign in Button: name="commit"

    How to Automate Login Using Selenium in Python? [Github Code]

    #Python automation program for Github login

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    
    driver = webdriver.Chrome(r"C:\Users\tsmehra\Desktop\code\chromedriver.exe")
    
    #open github.com login
    driver.get("http://www.github.com/login")
    
    #My githun credentials
    my_username ="MyUserName"
    my_password= "MyPassword123@"
    
    #access github login username input
    username_input_box = driver.find_element_by_name("login")
    
    #access github login password input
    password_input_box = driver.find_element_by_name("password")
    
    #access github signup button
    sign_up_button = driver.find_element_by_name("commit")
    
    #clear the placeholders data
    username_input_box.clear()
    password_input_box.clear()
    
    #fill login credentials
    username_input_box.send_keys(my_username)
    time.sleep(2)    #2 second time gap between filling username and password
    password_input_box.send_keys(my_password)
    
    time.sleep(2)    #2 second time delay
    
    #hit the login button
    sign_up_button.click()
    # automatically close the driver after 30 seconds
    time.sleep(30)
    driver.close()

    Conclusion

    Now you know how to use Selenium with Python to automate the login process. The only thing you need to figure out is the element name or any other attribute by which you could access the specific element in the webpage. Apart from the find_element_by_name() method, the web driver also provides many other methods to access an element with different attributes.

    In this Python tutorial, we only learned the login automation process with Selenium but it is capable of doing many other tasks. We highly suggest checking out Selenium in Python official documentation for more information.

    People are also reading:

    Leave a Comment on this Post

    0 Comments