我试图通过单击日期选择器日历中的下一个按钮来获得无家可归的每一天的价格,但是没有运气。
我当前的代码是这样的:
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个月。例如,对于家乡度假,我self.driver.find_element_by_css_selector('.ui- datepicker-next.ui-corner- all').click()在第一次打开日历点击后立即尝试使用,但我得到了ElementNotVisibleException
self.driver.find_element_by_css_selector('.ui- datepicker-next.ui-corner- all').click()
ElementNotVisibleException
提前致谢
我可以使用它,driver.execute_script('$( "a.ui-datepicker-next" ).click()')因为其他所有东西都给了我ElementNotVisibleException,然后我注意到其中涉及到JavaScript
driver.execute_script('$( "a.ui-datepicker-next" ).click()')
<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></span></a>
我宁愿不使用Javascript或jQuery直接使用,所以如果有人有更好的建议,我可以尝试一下。
Javascript
jQuery
但是仍然有一些怪癖,例如,每次我获得某个月的可用性时,我都必须手动重置日历。
这是我的最终代码:
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()