Tuesday, November 9, 2021

selenium 8 booking bot select date

after destination is entered, date table opens

select <td> element with css selector

checking in and checking out dates are selected
#Booking/booking.py
 
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()

-------------------------
#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-10', '2021-12-10')

reference:

No comments:

Post a Comment