1

i am trying to get the price for each day in homeaway 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 homeaway(self):
        display = Display(visible=0, size=(1024, 768))
        display.start()
        driver = webdriver.Firefox()
        driver.maximize_window()

        wait = WebDriverWait(driver, 10)

        url = 'https://www.homeaway.pt/arrendamento-ferias/p1418427a?uni_id=1590648'
        driver.get(url)

        # pick start date
        start_date = wait.until(EC.visibility_of_element_located((
                                                                 By.CSS_SELECTOR,
                                                                 ".quotebar-container input[name=startDateInput]")))
        start_date.click()

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))
        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # pick end date (TODO: violates DRY principle, refactor!)
        end_date = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR, ".quotebar-container input[name=endDateInput]")))
        end_date.click()

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))
        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # get the calculated price
        price = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR, ".price-quote .price-total")))

        print(price.text.encode('ascii', 'ignore'))

        driver.close()

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

psychok7
  • 5,373
  • 9
  • 63
  • 101

2 Answers2

1

To click on "Next month" button when datepicker is already opened try following:

def click_next_month(self):
    elements = self.driver.find_element_by_xpath('//span[@class="ui-icon ui-icon-circle-triangle-e"]')
    elements[1].click()

To click "Next month" button few times you need to re-define elements each time, so better to implement both actions (defining list and button click) as method click_next_month().

Try and let me know if any issues occurs

Andersson
  • 51,635
  • 17
  • 77
  • 129
1

I was able to get this working using driver.execute_script('$( "a.ui-datepicker-next" ).click()') since everything else was giving me ElementNotVisibleException and then i noticed that there was javascript involved

<a class="ui-datepicker-next ui-corner-all" data-handler="next" data-event="click" title="Próximo>"><span class="ui-icon ui-icon-circle-triangle-e">Próximo&gt;</span></a>

I would rather not use Javascript or jQuery directly so if anyone has better suggestions i can try them.

But there are still a few quirks, for example i have to manually reset the calendar each time i get the availability for a certain month.

here is my final code:

def homeaway(self):
    MONTH_COUNT = 6
    display = Display(visible=0, size=(1024, 768))
    display.start()
    driver = webdriver.Firefox()
    driver.maximize_window()

    wait = WebDriverWait(driver, 10)

    url = 'https://www.homeaway.pt/arrendamento-ferias/p1418427a?uni_id=1590648'
    driver.get(url)

    count = 0
    for month in range(MONTH_COUNT):
        # pick start date
        start_date = wait.until(EC.visibility_of_element_located((
            By.CSS_SELECTOR,
            ".quotebar-container input[name=startDateInput]")))
        start_date.click()

        for x in range(count):
            driver.execute_script('$( "a.ui-datepicker-next" ).click()')

        current_month = driver.find_element_by_css_selector(
            ".ui-datepicker-month").text

        print("current_month:", current_month)

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))

        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # pick end date (TODO: violates DRY principle, refactor!)
        end_date = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR,
             ".quotebar-container input[name=endDateInput]")))

        end_date.click()

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))

        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # get the calculated price
        price = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR, ".price-quote .price-total")))

        print(price.text.encode('ascii', 'ignore'))

        driver.execute_script('$( "button.ui-datepicker-clear" ).click()')

        count += 1

    driver.close()
psychok7
  • 5,373
  • 9
  • 63
  • 101