python selenium 日历控件_Python Selenium + Datepicker请点击

我一直在努力尝试获取此类房间的价格,例如,通过单击第一个可用的(绿色)datepicker签入输入,然后单击第一个可用的datepicker签入输入,以便生成最短期间的价格。

我的代码很乱,所以如果有人可以发布更简洁的代码来实现这一目标,我将不胜感激。

我正在使用Python selenium + scrapy,尽管以Java为例仍然有用。

更新:

这是代码:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值