Sunday, November 7, 2021

selenium 6 booking bot change currency


default currency is CAD

auto click currency button, and select GBP

currency is changed to GBP

select currency button by css selector

select <a> by css selector
#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()

----------------------------
#run.py

from Booking.booking import Booking

with Booking(auto_close=False) as bot:
    bot.open_page()
    bot.change_currency(currency='GBP')

reference:

conditional wait

No comments:

Post a Comment