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(https://www.google.com/travel/flights).

Step 1: Download the latest chrome driver

Go to https://chromedriver.storage.googleapis.com/index.html?path=95.0.4638.69/

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

Step 2: Clone repository and setup virtual env

Clone repo: https://github.com/dhruvrajkotia/automation_scripts

  • git clone git@github.com:dhruvrajkotia/automation_scripts.git
  • 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

python google_flights_automation_script.py

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 selenium.webdriver.common.by 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: https://selenium-python.readthedocs.io/

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 = "https://www.google.com/flights?hl=en"
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.maximize_window()
    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. (https://www.google.com/flights?hl=en)

   # source_city selection steps
    fly_from = driver.find_element(By.XPATH, source_city_XPATH)
    fly_from.click()
    fly_from_text = driver.find_element(By.XPATH, dropdown_selection_XPATH)
    fly_from_text.send_keys(source_city)
    fly_from_text.send_keys(Keys.ENTER)
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: https://www.browserstack.com/guide/find-element-by-xpath-in-selenium) 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.click()
    fly_to_text = driver.find_element(By.XPATH, dropdown_selection_XPATH)
    fly_to_text.send_keys(destination_city)
    fly_to_text.send_keys(Keys.ENTER)
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)
    departure_date_element.click()
    time.sleep(2)
    calendar_div = driver.find_element(By.XPATH, calendar_date_XPATH.format(date=departure_date))
    calendar_div.click() 
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))
    return_date_calendar_div.click()
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)
    submit_button.click()
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.

Oldest comments (1)

Collapse
 
syun profile image
syun

Hi, I am pretty noobie about all these.
I stuck at the "pip install -r requirements.txt"
The installation goes but i caught into error:

Building wheels for collected packages: cffi
Building wheel for cffi (pyproject.toml) ... error
error: subprocess-exited-with-error

× Building wheel for cffi (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [36 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-311
creating build/lib.linux-x86_64-cpython-311/cffi
copying cffi/lock.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/init.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/cparser.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/model.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/ffiplatform.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/vengine_gen.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/commontypes.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/pkgconfig.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/verifier.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/recompiler.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/error.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/api.py -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/cffi_include.h -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/parse_c_type.h -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/_embedding.h -> build/lib.linux-x86_64-cpython-311/cffi
copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-cpython-311/cffi
running build_ext
building '_cffi_backend' extension
creating build/temp.linux-x86_64-cpython-311
creating build/temp.linux-x86_64-cpython-311/c
x86_64-linux-gnu-gcc -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -g -fwrapv -O2 -fPIC -DUSE
_THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/home/yun/automation_scripts/automation_scripts/venv/include -I/usr/include/python3.11 -c c/_cffi_backend.c -o build/temp.linux-x86_64-cpython-311/c/_cffi_backend.o
c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
15 | #include
| ^~~~~~~
compilation terminated.
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for cffi
Failed to build cffi
ERROR: Could not build wheels for cffi, which is required to install pyproject.toml-based projects