我试图通过点击日期选择器日历中的下一个按钮来获取每天在家乡的价格,但没有运气。
我目前的代码如下:
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').click()
,但我得到了ElementNotVisibleException
提前致谢
答案 0 :(得分:1)
点击"下个月"当datepicker已经打开时按钮尝试以下:
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()
。
如果出现任何问题,请尝试告诉我
答案 1 :(得分: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></span></a>
我不想直接使用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()