Automate with Python & Selenium

In this article, we will know how to automate things using Python + Selenium. Here, we will automate a simple process of logging into any portal. Before we move into the process of automation first let us know what is Python and Selenium in a brief.

Python

As you all techies know, Python is a versatile and powerful programming language that is widely used for a variety of purposes. It supports multiple programming paradigms, including object-oriented, procedural, and functional programming. Also has a vast standard library that provides ready-to-use modules for tasks like file I/O, networking, web development, and more.

Selenium

Selenium is a widely used open-source framework for automating web browsers. It provides a programming interface that allows developers to control web browsers programmatically and simulate user interactions. Selenium is primarily used for testing web applications, but it also has other applications such as web scraping, browser automation, and automating repetitive tasks on the web.

Automate the Logging process (For Win OS)

Install Python

  • You can download Python from the python.org site. Click on the Downloads tab and download the appropriate version as per the OS you are using. Then Install the exe file.

  • If you are a Windows user, then open CMD Prompt and run python in the terminal to verify whether the Python is being installed.

Install Selenium

  • Once confirmed, run pip install selenium in the cmd prompt. This will install selenium on your machine.

Download Web Driver

  • Download the web driver of your personal choice. Make sure it should be of the same version of your browser. Here is the link for Web Driver Chrome. Below is the picture of version 113.0.5672.63. Then download it for your appropriate OS.

Code for Automation

Open your favorite editor (Eg: VS Code, Sublime) and write the Python script to automate the logging process.

  • Import some modules in your Python script: selenium, web driver, keys, By and time.

    Here;

    -> Web driver will allow you to use the browser

    -> Key will help you perform any event

    -> By will help you to find the element. (you can find the element by ID, CSS Selector, XPATH..)

    -> Time and sleep module will help to set time-sleep once the event is being performed.

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      from selenium.webdriver.common.by import By
      import time
      from time import sleep
    
  • Setup web driver: Here you need to give the path of the recently downloaded web driver.

      driver = webdriver.Chrome('/path/to/chromedriver')
    
  • Navigate to the login page: use the `get()` method to redirect to the desired URL. Copy and paste the URL of the Portal/Form which you to log in.

      driver.get('paste_your_URL_here')
    
  • Now to add the element you can first manually go to that portal and open up the inspector. Then select which element you want, for eg: here you are automating logging, so you will have to click the username field on the UI

and in the inspector it will highlight that part when you click on the username element in UI. From there you can copy XPATH.

  • Once copied, paste it in the code, to automate the login process. Similarly, do this for the password field also and give the input in the next code lines.

      email_field = driver.find_element(By.XPATH, '//*[@id="your-email-xpath"]')
      password_field = driver.find_element(By.XPATH, '//*[@id="your-password-xpath"]')
    
      email_field.send_keys('your_email@example.com')
      password_field.send_keys('your_password')
    
  • The next is to hit enter/Log in button: This code line is trying to hit Enter after the password is been inserted.

      password_field.send_keys(Keys.RETURN)
    
  • Set time sleep: Once the login is successful, you need the page to stop there itself and get closed. For that, you can set the time sleep limit.

      # Wait for 100 seconds
      time.sleep(100)
    
  • Now you can manually do the further things or can also write a script to automate those tasks.

  • To close the browser you can write this code line at the end of the script.

      driver.quit()
    

The Final Look of Code

# Adding some required modules
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from time import sleep

#opens web browser
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get('paste_your_URL_here')

#finds the username and password field on the whole page
email_field = driver.find_element(By.XPATH, '//*[@id="your-email-xpath"]')
password_field = driver.find_element(By.XPATH, '//*[@id="your-password-xpath"]')

#inserts values in place of username and password
email_field.send_keys('your_email@example.com')
password_field.send_keys('your_password')

# hits ENTER after filling the password field
password_field.send_keys(Keys.RETURN)

# Wait for 100 seconds
time.sleep(100)

# Write the further automation script here if required

# to close the browser
driver.quit()

So, that was it in this article. I hope this article was informative for you and you have learned the basic automation with Python & Selenium. You can add more to this and automate many various things and play around with Python & Selenium.

Thank you for reading this article.