DEV Community

Cover image for Automation Script for the Flight Searching
Dhruv Rajkotia
Dhruv Rajkotia

Posted on

Automation Script for the Flight Searching

How's it going guys😀, Today I wanted to share you how to write an automation script for any website. for this example I'm going to use the example of the Google Flight(

Step 1: Download the latest chrome driver

Go to

Download the chrome driver based on your OS(I'm using Windows💻)

Step 2: Clone repository and setup virtual env

Clone repo:

  • git clone
  • cd automation_scripts
  • python -m venv (I'm using Python 3.9)
  • pip install -r requirements.txt (Install all requirements)

Put your chrome driver in the chrome_driver directory in the project.

Great👏, You have setup the Project.

Step 3: Run Python Script


You can see that one new browser window will shown up and search for the flight based on the parameters that we have provided in the script.

Now you have successfully configured the script in your local file. Now let's talk about the script logic and how it's working. Hope 🤞 you enjoyed to see the result of the Automation script.

Code Explanation

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from import By
Enter fullscreen mode Exit fullscreen mode

So here we are using the selenium library for automation script. Selenium is one of the best library available for the Python Automation script. for more information check it out the official documentation of the selenium:

chromedriver_location = "chrome_driver/chromedriver.exe"  # Path of chrome driver script. OS: windows
Enter fullscreen mode Exit fullscreen mode

Here we have provided the path to the chromium driver.

# test inputs
site = ""
source_city = "Ahmeda"
destination_city = "Mumbai"
departure_date = "2021-11-21" # YYYY-MM-DD
return_date = "2022-02-27"

# XPATH List of the UI elements
source_city_XPATH = "//*[@id='i6']/div[1]/div/div/div[1]/div/div/input"
dropdown_selection_XPATH = "//*[@id='i6']/div[6]/div[2]/div[2]/div[1]/div/input"
destination_XPATH = "//*[@id='i6']/div[4]/div/div/div[1]/div/div/input"
departure_date_XPATH = '//*[@id="yDmH0d"]/c-wiz[2]/div/div[2]/div/c-wiz/div/c-wiz/div[2]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div/div/div[1]/div/div[1]/div/input'
return_date_XPATH = '//*[@id="yDmH0d"]/c-wiz[2]/div/div[2]/div/c-wiz/div/c-wiz/div[2]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div/div/div[1]/div/div[2]/div/input'
search_button_XPATH = '//*[@id="yDmH0d"]/c-wiz[2]/div/div[2]/div/c-wiz/div/c-wiz/div[2]/div[1]/div[2]/div/button'
calendar_date_XPATH = '//div[@data-iso = "{date}"]//div[@class="lkvzbb KQqAEc"]'
calendar_submit_button_XPATH = '//*[@id="ow59"]/div[2]/div/div[3]/div[3]/div/button'
Enter fullscreen mode Exit fullscreen mode

Here we have defined the static test input which will fill the values in the google flights website via script.

we have defined the XPATH for the UI component which will help us to fill the test values using automation script.

    driver = webdriver.Chrome(chromedriver_location)
    driver.get(site)  # Open Site in new chrome window
Enter fullscreen mode Exit fullscreen mode

So using above code new chrome window will open and then maximize the size of the chrome window to full size and open the site that we have configured in the test inputs. (

   # source_city selection steps
    fly_from = driver.find_element(By.XPATH, source_city_XPATH)
    fly_from_text = driver.find_element(By.XPATH, dropdown_selection_XPATH)
Enter fullscreen mode Exit fullscreen mode

So here we first find the source city element. For that you need to open the site and open the inspect window. Select the element and copy the XPATH for the element.(How to find XPATH: After then we click on that div so the dropdown will be open where we can search for the city name.

Image description

  # destination_city selection steps
    fly_to = driver.find_element(By.XPATH, destination_XPATH)
    fly_to_text = driver.find_element(By.XPATH, dropdown_selection_XPATH)
Enter fullscreen mode Exit fullscreen mode

For the destination city, we are going to follow the same sequence.

# departure_date selection steps
    departure_date_element = driver.find_element(By.XPATH, departure_date_XPATH)
    calendar_div = driver.find_element(By.XPATH, calendar_date_XPATH.format(date=departure_date)) 
Enter fullscreen mode Exit fullscreen mode

This is for the calendar picker, to pick the calendar date. So here we first open the calendar by clicking on the div. and select the date.

Here we are going to use the one of the element in div which is data-iso, which has the date in the format of YYYY-MM-DD . So based on that we can select the appropriate date.

calendar_date_XPATH = '//div[@data-iso = "{date}"]//div[@class="lkvzbb KQqAEc"]'

The same way we are going to perform for the return date.

 # return_date selection steps
    return_date_calendar_div = driver.find_element(By.XPATH, calendar_date_XPATH.format(date=return_date))
Enter fullscreen mode Exit fullscreen mode

Once we added all the information we need to click on the Done button in the calendar picker.

Image description

 # click on the Done button in calendar picker
    submit_button = driver.find_element(By.XPATH, calendar_submit_button_XPATH)
Enter fullscreen mode Exit fullscreen mode

So, Yeah Congratulations, You probably understood all the concepts regarding automation script for flight search.

Hope 🤞 you liked it, Feel free to reach out to me if you have any doubts.

Please Follow me on twitter, I'm regularly posting some information regarding the ChatBot development, NLP, Python, NodeJS. Glad to connect with you.

Top comments (0)