2016-04-19 26 views
2

thisのような部屋の価格を取得しようとすると頭が痛いのですが、最初に利用可能な(緑の)日付ピッカーのチェックイン入力をクリックしてから、最初の利用可能な日付ピッカーのチェックアウト入力をクリックしてください最小期間のために生成される。Python Selenium + Datepickerクリック

私のコードは、誰かがそれを達成するためにクリーンなコードを投稿することができれば私は本当に感謝混乱です。

例えばJavaで何かがまだ役立つだろうが、私はPythonのセレン+ scrapyを使用しています。

がUPDATE:ここ

コードです:

def availability(self, doc): 
    url = doc['url'] + '#calendar' 
    self.driver.get(url) 
    is_active = True 
    # We want to the availability/price for each day in a month. 
    availabilities = [] 

    # wait for the check in input to load 
    wait = WebDriverWait(self.driver, 10) 

    try: 
     elem = wait.until(
      EC.visibility_of_element_located(
       (By.CSS_SELECTOR, ".dates-group input[name=startDateInput]") 
      ) 
     ) 
    except TimeoutException: 
     pass 
    else: 
     elem.click() # open calendar 
     # wait for datepicker to load 
     wait.until(
      EC.visibility_of_element_located(
       (By.CSS_SELECTOR, '.ui-datepicker:not(.loading)')) 
     ) 
     days = self.driver.find_elements_by_css_selector(
      "#ui-datepicker-div tr td" 
     ) 

     for cell in days: 
      day = cell.text.strip() 
      if not day: 
       continue 

      if "full-changeover" not in cell.get_attribute("class"): 
       available = False 
      else: 
       available = True 

      self.logger.warning('CELL "%s"', cell) 
      self.logger.warning('DAY "%s"', day) 
      self.logger.warning('available "%s"', available) 


     # The first iteration was to list the availability, now we want to 
     # click the first available element to get the price 
     for cell in days: 
      day = cell.text.strip() 
      if not day: 
       continue 

      if "full-changeover" in cell.get_attribute("class"): 
       self.logger.warning('CLICK IT "%s"', day) 
       self.driver.implicitly_wait(10) 
       x = self.driver.find_element_by_xpath("//table/tbody/tr/td/a[text()=" + day + "]") 
       self.driver.implicitly_wait(10) 
       x.click() # Element not found in the cache issue here 
       # import ipdb; ipdb.set_trace() 

      # self.logger.warning('CELL "%s"', cell) 
      # self.logger.warning('DAY "%s"', day) 
      # self.logger.warning('available "%s"', available) 

     # elem.click() # close checkin calendar 

     # Now lets click on the checkout input to get the price and minimum 
     # number of days. We probably don't have to wait for the checkout 
     # because its already loaded but you never know. 

     try: 
      elem = wait.until(
       EC.visibility_of_element_located(
        (By.CSS_SELECTOR, 
        ".dates-group input[name=endDateInput]") 
       ) 
      ) 
     except TimeoutException: 
      pass 
     else: 
      # elem.click() # open calendar in checkout input 
      # wait for datepicker to load 
      wait.until(
       EC.visibility_of_element_located(
        (By.CSS_SELECTOR, '.ui-datepicker:not(.loading)')) 
      ) 
      days = self.driver.find_elements_by_css_selector(
       "#ui-datepicker-div tr td" 
      ) 

      for cell in days: 
       day = cell.text.strip() 
       if not day: 
        continue 

       # This is the first available date to checkout 
       if "full-changeover" in cell.get_attribute("class"): 
        self.logger.warning('CLICK IT "%s"', available) 
        import ipdb; ipdb.set_trace() 
        # Here we would get the generated price 



       self.logger.warning('CELL "%s"', cell) 
       self.logger.warning('DAY "%s"', day) 
       self.logger.warning('available "%s"', available) 




     import ipdb; ipdb.set_trace() 

    return {'availabilities': availabilities, 'is_active': is_active} 

おかげ

+0

これまでの投稿を投稿できますか?少なくとも、関連部分..私は、これは私はあなたが私に教えてくださいより良いアイデアを持っている場合、それは、動作させるために右のだろうと思ったものだけである方法によってコード – alecxe

+0

。私は何かを間違ってやっていることを私がどのように感じていたかを、クリックして「キャッシュには見つからない要素」を取得し始めました。 – psychok7

+0

@alecxeを追加@alecxe – psychok7

答えて

1

このカレンダーについての一つのトリッキーな事は、あなたが最初に特定の日にカーソルを移動して、アクティブな一日再配置する必要があるということで、クリックして。ここで算出した価格を最初に利用可能な開始日と終了日を選択して出力する作業を実装したものです:

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 


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) 

driver.close() 

瞬間は、それが20/04/201623/04/2016とプリント180€を選択します。

希望に役立ちます。

+0

私は今夜これを試してみて、病気にそれが動作右:) – psychok7

+0

後にそれをマークしてきましょう:) ...再びあなたの助けに感謝、私は状況を取得することができますどのようにこの溶液を用いて、 – psychok7

+0

こんにちはalecxeをこれを理解しようとしている全体の午後を過ごしましたfolowing月ですか?私は。(クリックしてください(「UI-DatePickerの-next.uiコーナー-すべて。」)self.driver.find_element_by_css_selector 'との最初のstart_dateのクリックした後、カレンダーの横のボタンをクリックしようとしています)'しかし、私は、 'ElementNotVisibleExceptionを取得しています「私が間違っていることは何ですか? – psychok7

関連する問題