Skip to content

Instantly share code, notes, and snippets.

@izaac
Last active December 24, 2015 04:30
Show Gist options
  • Save izaac/06cb7d558cac6adf54dc to your computer and use it in GitHub Desktop.
Save izaac/06cb7d558cac6adf54dc to your computer and use it in GitHub Desktop.
Search a flight lowest price
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.common.exceptions import NoSuchElementException
from locale import LC_ALL, atof, setlocale
import platform
import time
setlocale(LC_ALL, '')
class NavigateFlightSearchSite:
def __init__(self, bin_path=''):
if bin_path == '':
self._driver = webdriver.Firefox()
else:
ffbin = FirefoxBinary(firefox_path=bin_path)
self._driver = webdriver.Firefox(firefox_binary=ffbin)
self.wait = WebDriverWait(self._driver, timeout=60)
self._driver.implicitly_wait(5)
self.base_url = ''
def quit(self):
self.driver.quit()
@property
def driver(self):
return self._driver
@driver.setter
def driver(self, obj):
self._driver = obj
class NavigateOrbitzHomeSearch(NavigateFlightSearchSite):
def __init__(self, bin_path=''):
super(NavigateOrbitzHomeSearch, self).__init__(bin_path)
self.base_url = 'http://www.orbitz.com'
self.driver.get(self.base_url)
def search(self, orig='', dest=''):
driver = self.driver
radio_selection = driver.find_element(By.XPATH, "//div[@id='products']/div/fieldset/div[2]/label/input")
actions = ActionChains(driver)
actions.move_to_element(radio_selection)
actions.click()
actions.perform()
element = self.wait.until(EC.element_to_be_clickable((By.NAME,
'ar.rt.leaveSlice.orig.key'
)))
from_input = None
if element:
from_input = driver.find_element(By.NAME, 'ar.rt.leaveSlice.orig.key')
from_input.click()
from_input.clear()
from_input.send_keys(orig)
to_input = driver.find_element(By.NAME, 'ar.rt.leaveSlice.dest.key')
to_input.click()
to_input.clear()
to_input.send_keys(dest)
leave_date = driver.find_element(By.NAME, 'ar.rt.leaveSlice.date')
return_date = driver.find_element(By.NAME, 'ar.rt.returnSlice.date')
leave_date.send_keys('5/1/2015')
return_date.send_keys('5/10/2015')
send_search = driver.find_element(By.NAME, 'search')
send_search.click()
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.airResultsMod')))
class NavigateOrbitzSearchResults:
def __init__(self):
self._price = ''
self._driver = None
def get_best_price(self, driver):
all_prices = None
try:
self.price = driver.find_element(By.XPATH,
"//span[@class='money small-cents small-symbol']").text.lstrip('$')
all_prices = driver.find_elements(By.XPATH,
"//span[@class='money small-cents small-symbol']")
except NoSuchElementException as nsee:
from time import sleep
sleep(60)
print(nsee)
try:
self.price = atof(self.price)
except ValueError as ve:
print(ve)
print(len(all_prices))
print("All Costs")
for i in (x.text.lstrip('$') for x in all_prices):
print(i)
def __str__(self):
print(self.price)
return 'The minumum cost is: {0}'.format(self.price)
@property
def price(self):
return self._price
@price.setter
def price(self, obj):
self._price = obj
class NavigateMatrixFlightsHome(NavigateFlightSearchSite):
def __init__(self, bin_path=''):
super(NavigateMatrixFlightsHome, self).__init__(bin_path)
self.base_url = 'http://matrix.itasoftware.com/'
self.driver.get(self.base_url)
def search(self, orig='', dest=''):
from_input = self.driver.find_element(By.ID, 'cityPair-orig-0')
to_input = self.driver.find_element(By.ID, 'cityPair-dest-0')
if from_input.is_displayed():
from_input.send_keys(orig)
time.sleep(3)
actions = ActionChains(self.driver)
actions.key_down(Keys.ARROW_DOWN)
actions.key_down(Keys.RETURN)
actions.perform()
to_input.send_keys(dest)
time.sleep(3)
actions = ActionChains(self.driver)
actions.key_down(Keys.ARROW_DOWN)
actions.key_down(Keys.RETURN)
actions.perform()
leave_date = self.driver.find_element(By.XPATH, "//input[@id='cityPair-outDate-0']")
if leave_date.is_displayed():
leave_date.send_keys('5/1/2015')
leave_date.send_keys(Keys.TAB)
return_date = self.driver.find_element(By.XPATH, "//input[@id='cityPair-retDate-0']")
if return_date.is_displayed():
return_date.send_keys('5/10/2015')
return_date.send_keys(Keys.ESCAPE)
search_button = self.driver.find_element(By.XPATH, "//button[@id='searchButton-0']")
if search_button.is_displayed():
search_button.click()
class NavigateMatrixFlightsSearchResults:
def __init__(self):
self._price = ''
self._driver = None
self.wait = None
self._all_prices = []
def get_best_price(self, driver):
all_prices = ''
self.wait = WebDriverWait(driver, 60)
try:
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,
"button.GE-ODR-BOU")))
self._all_prices = driver.find_elements(By.XPATH, "//span[@class='GE-ODR-BMU']")
except NoSuchElementException as nsee:
from time import sleep
sleep(60)
print(nsee)
print(len(self._all_prices))
print("All Costs")
for i in (x.text.lstrip('$') for x in self._all_prices):
i = i.replace(' ', '')
if i:
print(i)
@property
def price(self):
return self._price
@price.setter
def price(self, obj):
self._price = obj
def __str__(self):
print('The minimum cost is: {0}'.format(self._all_prices[0]))
if __name__ == '__main__':
uname = platform.uname()
if uname.system == 'Windows':
home = NavigateOrbitzHomeSearch('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
else:
home = NavigateOrbitzHomeSearch()
origin = 'Tucson, Arizona - Tucson International Airport (TUS)'
destination = 'Paris, France - Charles de Gaulle Airport (CDG)'
home.search(orig=origin, dest=destination)
results = NavigateOrbitzSearchResults()
results.get_best_price(home.driver)
home.quit()
# WIP ITA MATRIX
if uname.system == 'Windows':
home = NavigateMatrixFlightsHome('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
else:
home = NavigateMatrixFlightsHome()
origin = 'TUS'
destination = 'CDG'
home.search(orig=origin, dest=destination)
matrix_results = NavigateMatrixFlightsSearchResults()
matrix_results.get_best_price(home.driver)
print(matrix_results)
home.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment