default booking option 2 adults, 1 room
from time import sleep
import Booking.constants as constants
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Booking(webdriver.Chrome):
def __init__(self, path=r"C:/Users/zchen/PycharmProjects/selenium", auto_close=True):
self.driver_path = path
self.auto_close = auto_close
os.environ['PATH'] += self.driver_path
super(Booking, self).__init__()
def __exit__(self, exc_type, exc_val, exc_tb):
if self.auto_close:
print('exiting...')
self.quit()
def open_page(self):
self.get(constants.BASE_URL)
print(constants.BASE_URL, 'is opened')
def change_currency(self, currency=None):
currency_element = self.find_element(
By.CSS_SELECTOR,
'button[data-tooltip-text="Choose your currency"]'
)
currency_element.click()
WebDriverWait(self, 30).until(
EC.presence_of_element_located(
(
By.CSS_SELECTOR,
f'a[data-modal-header-async-url-param*={currency}]'
)
)
)
selected_currency_element = self.find_element(
By.CSS_SELECTOR,
f'a[data-modal-header-async-url-param*={currency}]'
)
selected_currency_element.click()
def select_place_to_go(self, place_to_go):
search_field = self.find_element(
By.ID,
"ss"
)
search_field.clear()
search_field.send_keys(place_to_go)
WebDriverWait(self, 30).until(
EC.presence_of_element_located(
(
By.CSS_SELECTOR,
'li[data-i="0"]'
)
)
)
first_result = self.find_element(
By.CSS_SELECTOR,
'li[data-i="0"]'
)
first_result.click()
def select_dates(self, check_in, check_out):
check_in_element = self.find_element(
By.CSS_SELECTOR,
f'td[data-date="{check_in}"]'
)
check_in_element.click()
check_out_element = self.find_element(
By.CSS_SELECTOR,
f'td[data-date="{check_out}"]'
)
check_out_element.click()
def update_adult_room_number(self, adult_num, room_num):
expand_button = self.find_element(
By.ID,
'xp__guests__toggle'
)
expand_button.click()
def find_current_adult_num():
return int(self.find_element(
By.ID,
'group_adults'
).get_attribute('value'))
def find_current_room_num():
return int(self.find_element(
By.ID,
'no_rooms'
).get_attribute('value'))
increase_adult_num_button = self.find_element(
By.CSS_SELECTOR,
'button[aria-label="Increase number of Adults"]'
)
decrease_adult_num_button = self.find_element(
By.CSS_SELECTOR,
'button[aria-label="Decrease number of Adults"]'
)
increase_room_num_button = self.find_element(
By.CSS_SELECTOR,
'button[aria-label="Increase number of Rooms"]'
)
decrease_room_num_button = self.find_element(
By.CSS_SELECTOR,
'button[aria-label="Decrease number of Rooms"]'
)
while find_current_room_num() != room_num:
if find_current_room_num() > room_num:
decrease_room_num_button.click()
else:
increase_room_num_button.click()
while find_current_adult_num() != adult_num:
if find_current_adult_num() > adult_num:
decrease_adult_num_button.click()
else:
increase_adult_num_button.click()
def search(self):
search_button = self.find_element(
By.CSS_SELECTOR,
'button[data-sb-id="main"]'
)
search_button.click()
----------------------------
#run.py
from Booking.booking import Booking
with Booking(auto_close=False) as bot:
bot.open_page()
#bot.change_currency(currency='GBP')
bot.select_place_to_go("New York")
bot.select_dates('2021-11-15', '2021-12-15')
bot.update_adult_room_number(1, 3)
bot.search()
reference: