Firefox可以工作,但是PhantomJS抛出无法找到带有css选择器的元素

时间:2016-08-30 13:43:06

标签: python python-3.x selenium datepicker phantomjs

我最近从webdriver.Firefox()更改为webdriver.PhantomJS()以获得速度提升,当我尝试在我的日期选择器上找到一个元素以便在

之后点击它时,我开始遇到一些错误
    self.driver = webdriver.PhantomJS()
    self.driver.set_window_size(1280, 1024)
    self.driver.find_element_by_css_selector(
                "#ui-datepicker-div td.full-selected.full-changeover > a"
            ).click()

Message: {"errorMessage":"Unable to find element with css selector '#ui-datepicker-div td.full-selected.full-changeover > a'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"146","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:54784","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"value\": \"#ui-datepicker-div td.full-selected.full-changeover > a\", \"sessionId\": \"8b584560-6eb6-11e6-bb4a-77906b62d5cb\", \"using\": \"css selector\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8b584560-6eb6-11e6-bb4a-77906b62d5cb/element"}}
Screenshot: available via screen

我正在使用selenium==2.53.6phantomjs==2.1.12

更新(这是代码):

def get_price(self, url):
    url = "https://www.homeaway.pt/arrendamento-ferias/p1823902"
    # Lets reset it
    self.driver.get(url)

    wait = WebDriverWait(self.driver, 5)

    prices = defaultdict(list)

    count = 0
    for month in range(self.month_count):
        next_month_iteration = False
        checkin_date = wait.until(
            EC.visibility_of_element_located(
                (
                    By.CSS_SELECTOR,
                    ".quotebar-container input[id=startDateInput]"
                )
            )
        )
        checkin_date.click()

        for counting in range(count):
            try:
                self.driver.execute_script(
                    '$( "a.ui-datepicker-next" ).click()'
                )
            except WebDriverException:
                log.error(
                    'WebDriverException: Message: '
                    'getAvailabilityIndexForDate requires a Date object'
                )
                next_month_iteration = True
                break

        if next_month_iteration:
            # Skip the next iteration cause there was an error
            continue

        year = self.driver.find_element_by_css_selector(
            ".ui-datepicker-year").text

        current_month = self.driver.find_element_by_css_selector(
            ".ui-datepicker-month").text

        log.info(
            'Current Month is "%s"',
            current_month.encode('ascii', 'ignore').decode('utf-8')
        )

        try:
            first_available_checkin_date = wait.until(
                EC.element_to_be_clickable(
                    (
                        By.CSS_SELECTOR,
                        "#ui-datepicker-div td.full-changeover > a"
                    )
                )
            )
        except TimeoutException:
            log.warning('Is there any date available here "%s" ?', url)
            continue
        else:
            log.info(
                'First_available_checkin_date is "%s"',
                first_available_checkin_date.text
            )
            ActionChains(self.driver).move_to_element(
                first_available_checkin_date).perform()

            # self.driver.find_element_by_css_selector(
            #     "#ui-datepicker-div td.full-selected.full-changeover > a"
            # ).click()

            choose_date = wait.until(
                EC.visibility_of_element_located(
                    (
                        By.CSS_SELECTOR,
                        "#ui-datepicker-div td.full-selected.full-changeover > a"
                    )
                )
            )
            choose_date.click()

我有什么想法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我已经重现了您的问题,并且能够通过最大化浏览器窗口来修复它

self.driver = webdriver.PhantomJS()
self.driver.maximize_window()