Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium+;日期选择器点击_Python_Selenium_Datepicker_Scrapy - Fatal编程技术网

Python Selenium+;日期选择器点击

Python Selenium+;日期选择器点击,python,selenium,datepicker,scrapy,Python,Selenium,Datepicker,Scrapy,我一直在绞尽脑汁想得到一个房间的价格,例如,通过单击第一个可用(绿色)日期选择器签入输入,然后单击第一个可用日期选择器签入输入,生成最小期间的价格 我的代码乱七八糟,所以如果有人能发布一个更干净的代码来实现这一点,我将不胜感激 我使用的是Python selenium+scrapy,尽管Java中的一些示例仍然有帮助 更新: 代码如下: def availability(self, doc): url = doc['url'] + '#calendar' self.driver.

我一直在绞尽脑汁想得到一个房间的价格,例如,通过单击第一个可用(绿色)日期选择器签入输入,然后单击第一个可用日期选择器签入输入,生成最小期间的价格

我的代码乱七八糟,所以如果有人能发布一个更干净的代码来实现这一点,我将不胜感激

我使用的是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}

谢谢

此日历的一个棘手问题是,您首先需要将鼠标悬停在某一特定日期,然后重新定位活动日期并单击它。以下是一个工作实现,它选择第一个可用的开始和结束日期,并打印计算出的价格:

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()
目前,它选择了
20/04/2016
23/04/2016
并打印
180€


希望这能有所帮助。

您能发布您目前的资料吗?至少,相关部分。@alecxe我添加了code@alecxe顺便说一下,这正是我认为正确的做法,如果你有更好的想法,请让我知道。我在点击时开始在缓存中找到
元素
,这就是我意识到我做错了什么的原因。今晚让我试试这个,我马上来标记:)它工作:)。。再次感谢您花了整整一个下午的时间试图解决这个问题Hi-alecxe,使用此解决方案,我如何才能获得接下来几个月的可用性?我试图在第一次开始日期后单击“日历下一步”按钮,使用
self.driver。通过css\u选择器('.ui-datepicker-next.ui-corner-all')查找元素。单击()
,但我得到了一个
ElementNotVisibleException
知道我做错了什么吗?@psychok7嘿,你可以创建一个单独的问题吗?更多的用户将有机会提供帮助。我一定会看一看,给我一个链接。谢谢问题是。我有两个不同的网站,但代码都有麻烦