Python Selenium + Datepicker Click

后端 未结 1 1263
情书的邮戳
情书的邮戳 2021-01-06 04:21

I have been banging my head around trying to get the price of a room like this for example by clicking the first available (green) datepicker checkin input and then clicking

相关标签:
1条回答
  • 2021-01-06 04:57

    One tricky thing about this calendar is that you first need to hover a particular day and then relocate the active day and click it. Here is a working implementation that selects the first available start and end dates and prints the calculated price:

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

    At the moment, it selects 20/04/2016 and 23/04/2016 and prints 180€.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题