Python Selenium + Datepicker点击 [英] Python Selenium + Datepicker Click

查看:445
本文介绍了Python Selenium + Datepicker点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在打扰我的头,试图获得一个房间的价格,例如例如,通过单击第一个可用(绿色)日期戳检入输入,然后单击第一个可用的日期戳检查输入,从而生成最小期限的价格。



我的代码是一团糟,所以我真的很感激,如果有人可以发布一个更干净的代码来实现。



我正在使用Python selenium + scrapy,尽管Java中的某些内容仍然有帮助。



更新: / p>

这里是代码:

  def availability(self,doc) :
url = doc ['url'] +'#calendar'
self.driver.get(url)
is_active = True
#我们希望可用性/每个月一个月。
availabilities = []

#等待检入输入加载
wait = WebDriverWait(self.driver,10)

try:
elem = wait.until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR,.dates-group input [name = startDateInput])


除了TimeoutException:
pass
else:
elem.click()#打开日历
#等待datepicker加载
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


单元格天数:
day = cell.text.strip()
如果不是天:
continue

如果full-toggleover不在cell.get_attribute(cl屁股):
available = False
else:
available = True

self.logger.warning('CELL%s', $ b self.logger.warning('DAY%s',day)
self.logger.warning('available'%s',available)


#第一次迭代是列出可用性,现在我们要
#单击第一个可用元素以获取单元格中的
的价格:
day = cell.text.strip()
如果不是天:
继续

如果cell.get_attribute(class)中的full-changeover:
self.logger.warning('点击它%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()#元素在缓存问题在这里
#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()#关闭签入日历

#现在请点击结帐输入获取价格和最低
#天数。我们可能不必等待结帐
#,因为它已经加载,但你永远都不知道。

try:
elem = wait.until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR,
.dates-group input [name = endDateInput])


除了TimeoutException:
pass
else:
#elem.click()#在结帐输入中打开日历
#等待datepicker加载
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


单元格天数:
day = cell.text.strip()
如果不是日期:
继续

#这是第一个可用的检查日期
如果cell.get_attribute(class)中的full-changeover:
self.logger.warning('CLICK IT'%s'可用)
import ipdb; ipdb.set_trace()
#这里我们可以得到生成的价格



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}

谢谢

解决方案

这个日历的一个棘手的事情是你首先需要徘徊一个特定的日子重新安置活动日,然后单击它。这是一个工作实施,选择第一个可用的开始和结束日期并打印计算的价格:

 从selenium import webdriver $ b来自selenium.webdriver import的
$ s从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support import expected_conditions作为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)

#选择开始日期
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_el ement(first_available_date).perform()
driver.find_element_by_css_selector(#ui-datepicker-div td.full-selected.full-changeover>一个)。click()

#选择结束日期(TODO:违反DRY原则,重构!)
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- select.full-changeover> a)。click()

#获取计算价格
price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,.price-报价.price-total)))
print(price.text)

driver.close()

此刻,它选择 20/04/2016 23/04/2016 并打印 180€



希望有所帮助。


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 the first available datepicker checkout input so the price for the minium period is generated.

My code is a mess so i would really appreciate if someone could post a cleaner code to achieve that.

I am using Python selenium + scrapy although something in Java for example would still help.

UPDATE:

here is the code:

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}

Thanks

解决方案

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.

这篇关于Python Selenium + Datepicker点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆