Saturday, November 13, 2021

selenium 10 booking bot filters


select star rating group with css selector

loop through options in the group and check based on preference

3 to 5 stars are checked
#logs

(3, 4, 5)
6
3 checked
4 checked
5 checked

-----------------------------
#Booking/filters.py

from typing import List
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.by import By

class BookingFilters:
    def __init__(self, driver:WebDriver):
        self.driver = driver

    def star_rating(self, *star_list):
        star_group = self.driver.find_element(
            By.CSS_SELECTOR,
            'div[data-filters-group="class"]'
        )

        star_elements = star_group.find_elements(
            By.CSS_SELECTOR,
            'div[data-filters-item^="class:class="]'
        )

        print(star_list)
        print(len(star_elements))

        for star in star_list:
            for star_element in star_elements:
                if str(star) in star_element.get_attribute('data-filters-item'):
                    print(star, "checked")
                    star_element.click()

----------------------
#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
from Booking.filters import BookingFilters

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 filters(self, *stars):
        filters = BookingFilters(driver=self)
        filters.star_rating(*stars)

----------------------------
#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-20', '2021-11-25')
    bot.update_adult_room_number(1, 3)
    bot.search()
    bot.filters(3, 4, 5)

reference:

No comments:

Post a Comment