-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRyanairWindow.py
130 lines (97 loc) · 4.93 KB
/
RyanairWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import datetime
from airport_data import Airports
from Window import Window
from datetime import timedelta
from selenium.webdriver.common.by import By
class RyanairWindow(Window):
def __init__(self, browser_name):
website_address = "https://www.ryanair.com/"
super().__init__(browser_name, website_address)
self.__ryanair_window = super().getWindow()
def handleCookies(self):
"""
Accepts only mandatory cookies
:return: None
"""
# accepting cookies - only selected cookies (only those mandatory)
self.waitToLoadSiteContent(2)
# cookies = self.__ryanair_window.find_element_by_xpath('/html/body/div/div/div[3]/button[1]')
cookies = self.findElementByXPATHAndClick('/html/body/div/div/div[3]/button[1]')
# cookies.click()
self.waitToLoadSiteContent(2)
# switch to iframe
self.switchFrames("//iframe[@id='cookie-preferences__iframe']")
self.waitToLoadSiteContent(4)
# click toggle button to uncheck the unnecessary cookies
self.findElementByXPATHAndDoubleClick('//cookies-details/div/div[7]/div/ry-toggle/label/div/div/div')
# confirm choices
self.findElementByXPATHAndClick('//cookies-root/ng-component/main/section/div/cookies-details/div/button')
print('Cookies accepted.')
self.waitToLoadSiteContent(3)
# switch back to the outer window
self.__ryanair_window.switch_to.default_content()
def findEditSearchBtnAndClick(self):
"""
finds 'Edit Search' button and clicks it
"""
edit_search_btn = self.__ryanair_window.find_element_by_xpath(
"//button[contains(@class,'details__edit-search')]")
edit_search_btn.click()
def findCalendarAndClick(self):
"""
finds Calendar after clicking 'Edit Search' button and clicks it
"""
edit_search_choose_date = self.__ryanair_window.find_element(By.CSS_SELECTOR,
'div.flight-widget-controls__calendar')
edit_search_choose_date.click()
def searchForFlight(self, departure_city, destination_city, start_date):
"""
searching for a given flight
:param departure_city:
:param destination_city:
:param start_date: search start date user has chosen
:return: actual date when the search begins (so if the given date is not available, the nearest next one)
"""
# selecting one-way trip
self.findElementByXPATHAndDoubleClick(
"//fsw-trip-type-button[@data-ref='flight-search-trip-type__one-way-trip']/button")
print('One-way trip chosen.')
# choosing departure country
self.waitToLoadSiteContent(1)
self.findElementByXPATHAndDoubleClick("//*[@id='input-button__departure']")
self.waitToLoadSiteContent(1)
self.findElementByXPATHAndClick("//div[contains(@class,'countries__country')]/span[text()[contains(.,'" +
Airports[departure_city].country + "')]]")
self.waitToLoadSiteContent(1)
# choosing departure city
self.findElementByXPATHAndClick(
"//span[@data-id='" + Airports[departure_city].IATA_code + "']")
print('Departure city: ' + departure_city + '.')
self.waitToLoadSiteContent(1)
# choosing destination country
self.findElementByXPATHAndDoubleClick("//*[@id='input-button__destination']")
self.waitToLoadSiteContent(1)
self.findElementByXPATHAndClick(
"//div[contains(@class, 'countries__country')]/span[text()[contains(.,'"
+ Airports[destination_city].country + "')]]")
self.waitToLoadSiteContent(1)
# choosing destination city
self.findElementByXPATHAndClick(
"//span[@data-id='" + Airports[destination_city].IATA_code + "']")
print('Destination city: ' + destination_city + '.')
self.waitToLoadSiteContent(1)
# choosing start date (at least 10 days from today's date)
is_first_day_chosen = False
# if there is no flight on the argument date, the next available date will be the start date
while not is_first_day_chosen:
depart_day_div = self.__ryanair_window.find_element(By.CSS_SELECTOR,
"div.calendar-body__cell[data-id='" + str(
start_date) + "']")
is_first_day_chosen = self.isElementClickable(depart_day_div)
if not is_first_day_chosen:
start_date = start_date + timedelta(days=1)
print('Search start date: ' + str(start_date) + '.')
# clicking 'Find flights' button
self.findElementByXPATHAndClick("//button[contains(@class, 'flight-search-widget__start-search')]")
self.waitToLoadSiteContent(2)
return start_date