2016-05-04 63 views
1

我想通過點擊datepicker日曆中的下一個按鈕來獲取homeaway中每一天的價格,但沒有運氣。從datepicker獲取可用性

我當前的代碼是一樣的東西:

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() 

他們都工作,但只有1個月。我希望能夠設置X個月。例如,對於HomeAway的我試着用self.driver.find_element_by_css_selector('.ui-datepicker-next.ui-corner-all').c‌​lick()首開日曆點擊後的權利,但我得到了提前

回答

1

我能得到的任何問題,這一點使用driver.execute_script('$("a.ui-datepicker-next").click()')因爲一切是給我ElementNotVisibleException工作,然後我注意到,有JavaScript的參與

<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> 

我寧可不直接使用JavascriptjQuery,所以如果有人有更好的建議,我可以嘗試。

但仍然有一些怪癖,例如我不得不手動重置日曆,每次我得到一個月的可用性。

這裏是我的最終代碼:

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() 
1

一個ElementNotVisibleException

感謝點擊「下個月」按鈕,當日期選擇器已經打開嘗試以下操作:

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

要點擊「下個月」按鈕幾次,您需要每次重新定義elements,因此更好地實施這兩個操作(定義列表和按鈕單擊)作爲方法click_next_month()

嘗試,讓我知道,如果發生

+0

我會嘗試以後,讓你知道,謝謝 – psychok7

+0

嗨,我仍然得到了'ElementNotVisibleException'與您的代碼:( – psychok7