psychok7
psychok7

Reputation: 5473

Getting availability from datepicker for x months for a site

i am trying to get the availability/price for each day in airbnb by clicking the next button in the datepicker calendar but with no luck.

My current code is something like:

def handle(self, *args, **options):
    def airbnb():
        display = Display(visible=0, size=(1024, 768))
        display.start()
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get("https://www.airbnb.pt/rooms/265820")

        # wait for the check in input to load
        wait = WebDriverWait(driver, 10)
        elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
        elem.click()

        # wait for datepicker to load
        wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
        )

        days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
        for cell in days:
            day = cell.text.strip()
            if not day:
                continue

            if "ui-datepicker-unselectable" in cell.get_attribute("class"):
                status = "Unavailable"
            else:
                status = "Available"

            price = "n/a"
            if status == "Available":
                # hover the cell and wait for the tooltip
                ActionChains(driver).move_to_element(cell).perform()
                price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text

            print(day, status, price)

They both work but only for 1 month. I want to be able to set X months instead. For example for homeaway i tried with self.driver.find_element_by_css_selector('.ui-datepicker-next.ui-corner-all').c‌​lick() right after the first open calendar click but i got a ElementNotVisibleException

Thanks in advance

Upvotes: 1

Views: 687

Answers (1)

alecxe
alecxe

Reputation: 473813

First of all, I would locate the "next month" button with a.ui-datepicker-next CSS selector which is both readable and reliable.

Here is the implementation - processing as many months as the MONTH_COUNT variable defines:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


MONTH_COUNT = 3

driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://www.airbnb.pt/rooms/265820")

# wait for the check in input to load
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
elem.click()

# iterate over the month count
for month in range(MONTH_COUNT):
    # wait for datepicker to load
    wait.until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
    )

    # getting current month for displaying purposes
    current_month = driver.find_element_by_css_selector(".ui-datepicker-month").text
    print(current_month)

    # iterate over days
    days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
    for cell in days:
        day = cell.text.strip()
        if not day:
            continue

        if "ui-datepicker-unselectable" in cell.get_attribute("class"):
            status = "Unavailable"
        else:
            status = "Available"

        price = "n/a"
        if status == "Available":
            # hover the cell and wait for the tooltip
            ActionChains(driver).move_to_element(cell).perform()
            price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text

        print(day, status, price)

    print("-----")
    # click next month
    driver.find_element_by_css_selector("a.ui-datepicker-next").click()

driver.close()

Prints:

Maio
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'30', 'Unavailable', 'n/a')
(u'31', 'Unavailable', 'n/a')
-----
Junho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'28', 'Unavailable', 'n/a')
(u'29', 'Unavailable', 'n/a')
(u'30', 'Unavailable', 'n/a')
-----
Julho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'29', 'Unavailable', 'n/a')
(u'30', 'Available', u'\u20ac36')
(u'31', 'Available', u'\u20ac36')
-----

Upvotes: 1

Related Questions