问题描述
我正在尝试通过点击datepicker日历中的下一个按钮来获取airbnb每天的可用性/价格,但没有运气。
我当前的代码是如下:
def handle(self,* args,** options):
def airbnb():
display = Display(visible = 0,size =(1024,768))
display.start()
driver = webdriver.Firefox()
driver.maximize_window()
driver.get(https://www.airbnb.pt/rooms/265820)
#等待检入输入加载
wait = WebDriverWait(driver,10 )
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,div.book-it-panel input [name = checkin])))
elem.click()
#等待datepicker加载
wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR,'.ui-datepicker:not(.loading)'))
)
days = driver.find_ele ments_by_css_selector(。ui-datepicker table.ui-datepicker-calendar tr td)
单元格天数:
day = cell.text.strip()
如果不是日期:
continue
如果cell.get_attribute(class)中的ui-datepicker-unselectable:
status =不可用
else:
status = 可用
price =n / a
如果status ==可用:
#悬停单元格并等待工具提示
ActionChains(驱动程序).move_to_element(cell).perform()
price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'.datepicker-tooltip')))。text
print(日,状态,价格)
他们都工作但只有1个月。我想要能够设置X个月。例如,对于homeaway,我尝试使用 self.driver.find_element_by_css_selector('。ui-datepicker-next.ui-corner-all')。c lick()
首先打开日历点击,但我有一个 ElementNotVisibleException
提前感谢
首先,我将找到下个月按钮与 a.ui-datepicker-next
CSS选择器既可读又可靠。
这是实现 - 处理与$ code> MONTH_COUNT 变量定义的多个月:
$ b $来自selenium import webdriver的从selenium.webdriver导入ActionBains
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
MONTH_COUNT = 3
driver = webdriver。 Firefox()
driver.maximize_window()
driver.get(https://www.airbnb.pt/rooms/265820)
#等待检查输入加载
wait = WebDriverWait(driver,10)
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,div.book-it-panel input [name = checkin]) ))
elem.click()
#遍历月份计数
范围内的月份(MONTH_COUNT):
#等待datepicker加载
wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR,'.ui -datepicker:not(.loading)'))
)
#获取当前月份显示目的
current_month = driver.find_element_by_css_selector(。ui-datepicker-month) .text
print(current_month)
#iterate over days
days = driver.find_elements_by_css_selector(。ui-datepicker table.ui-datepicker-calendar tr td)
for cell in days:
day = cell.text.strip()
if not day:
continue
如果ui-datepicker-unselectablein cell.get_attribute(class):
status =不可用
else:
status =可用
price =n / a
如果status ==可用:
#悬停单元格并等待工具提示
ActionChains(驱动程序).move_to_element(cell).perform()
price = wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR,'.datepicker-tooltip')))。text
print(day,status,price)
print(-----)
#点击下个月
driver.find_element_by_css_selector(a.ui-datepicker-next ).click()
driver.close()
打印:
Maio
(u'1','不可用','n / a')
u'2','不可用','n / a')
(u'3','不可用','n / a')
...
(u'30 ','不可用','n / a')
(u'31','不可用','n / a')
-----
Junho
(u'1','不可用','n / a')
(u'2','不可用','n / a')
(u'3','不可用' 'n / a')
...
(u'28','不可用','n / a')
(u'29','不可用','n / a')
(u'30','不可用','n / a')
-----
Julho
(u'1','Unavailable' ,'n / a')
(u'2','不可用','n / a')
(u'3','不可用','n / a')
...
(u'29', '不可用','n / a')
(u'30','Available',u'\\\€36')
(u'31','Available',u'\\\€36 ')
-----
i am trying to get the availability/price for each day in airbnb by clicking the next button in the datepicker calendar but with no luck.
My current code is something like:
def handle(self, *args, **options):
def airbnb():
display = Display(visible=0, size=(1024, 768))
display.start()
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://www.airbnb.pt/rooms/265820")
# wait for the check in input to load
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
elem.click()
# wait for datepicker to load
wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
)
days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
for cell in days:
day = cell.text.strip()
if not day:
continue
if "ui-datepicker-unselectable" in cell.get_attribute("class"):
status = "Unavailable"
else:
status = "Available"
price = "n/a"
if status == "Available":
# hover the cell and wait for the tooltip
ActionChains(driver).move_to_element(cell).perform()
price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text
print(day, status, price)
They both work but only for 1 month. I want to be able to set X months instead. For example for homeaway i tried with self.driver.find_element_by_css_selector('.ui-datepicker-next.ui-corner-all').click()
right after the first open calendar click but i got a ElementNotVisibleException
Thanks in advance
First of all, I would locate the "next month" button with a.ui-datepicker-next
CSS selector which is both readable and reliable.
Here is the implementation - processing as many months as the MONTH_COUNT
variable defines:
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
MONTH_COUNT = 3
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://www.airbnb.pt/rooms/265820")
# wait for the check in input to load
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
elem.click()
# iterate over the month count
for month in range(MONTH_COUNT):
# wait for datepicker to load
wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
)
# getting current month for displaying purposes
current_month = driver.find_element_by_css_selector(".ui-datepicker-month").text
print(current_month)
# iterate over days
days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
for cell in days:
day = cell.text.strip()
if not day:
continue
if "ui-datepicker-unselectable" in cell.get_attribute("class"):
status = "Unavailable"
else:
status = "Available"
price = "n/a"
if status == "Available":
# hover the cell and wait for the tooltip
ActionChains(driver).move_to_element(cell).perform()
price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text
print(day, status, price)
print("-----")
# click next month
driver.find_element_by_css_selector("a.ui-datepicker-next").click()
driver.close()
Prints:
Maio
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'30', 'Unavailable', 'n/a')
(u'31', 'Unavailable', 'n/a')
-----
Junho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'28', 'Unavailable', 'n/a')
(u'29', 'Unavailable', 'n/a')
(u'30', 'Unavailable', 'n/a')
-----
Julho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'29', 'Unavailable', 'n/a')
(u'30', 'Available', u'\u20ac36')
(u'31', 'Available', u'\u20ac36')
-----
这篇关于从站点的x个月的datepicker获取可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!