Example #1
0
    async def goto(self,
                   url: str,
                   options: dict = None,
                   **kwargs: Any) -> Optional[Response]:
        """Got to url."""
        if options is None:
            options = dict()
        options.update(kwargs)
        watcher = NavigatorWatcher(self._client, self._ignoreHTTPSErrors,
                                   options)
        responses: Dict[str, Response] = dict()
        listener = helper.addEventListener(
            self._networkManager, NetworkManager.Events.Response,
            lambda response: responses.__setitem__(response.url, response))
        navigationPromise = watcher.waitForNavigation()
        referrer = self._networkManager.extraHTTPHeaders().get('referer', '')

        try:
            await self._client.send('Page.navigate',
                                    dict(url=url, referrer=referrer))
        except Exception:
            watcher.cancel()
            helper.removeEventListeners([listener])
            raise
        error = await navigationPromise
        helper.removeEventListeners([listener])
        if error:
            raise error

        if self._frameManager.isMainFrameLoadingFailed():
            raise PageError('Failed to navigate: ' + url)
        return responses.get(self.url)
Example #2
0
    async def goto(self,
                   url: str,
                   options: dict = None,
                   **kwargs: Any) -> Optional[Response]:
        """Go to the url.

        :arg string url: URL to go.
        """
        options = merge_dict(options, kwargs)
        referrer = self._networkManager.extraHTTPHeaders().get('referer', '')
        requests: Dict[str, Request] = dict()
        eventListeners = [
            helper.addEventListener(
                self._networkManager, NetworkManager.Events.Request,
                lambda request: requests.__setitem__(request.url, request))
        ]

        mainFrame = self._frameManager.mainFrame
        if mainFrame is None:
            raise PageError('No main frame.')
        watcher = NavigatorWatcher(self._frameManager, mainFrame, options)

        result = await self._navigate(url, referrer)
        if result is not None:
            raise PageError(result)
        result = await watcher.navigationPromise()
        watcher.cancel()
        helper.removeEventListeners(eventListeners)
        error = result[0].pop().exception()  # type: ignore
        if error:
            raise error

        request = requests.get(mainFrame.url)
        return request.response if request else None
Example #3
0
    async def goto(self, url, options={}):
        watcher = NavigatorWatcher(self._client, self._ignore_https_errors,
                                   options)
        responses = {}

        listener = Helper.add_event_listener(self._network_manager,
                                             NetworkManager.Events['Response'],
                                             self._get_scope(responses))
        result = watcher.wait_for_navigation()

        referrer = self._network_manager.extra_http_headers().get(
            'referer', '')
        try:
            await self._client.send('Page.navigate', {
                'url': url,
                'referrer': referrer
            })
        except Exception as e:
            watcher.cancel()
            raise e
        await result
        Helper.remove_event_listeners([listener])
        if self._frame_manager.is_main_frame_loading_failed():
            raise Exception('Failed to navigate: {}'.format(url))
        return responses.get(self.main_frame().url(), None)
Example #4
0
    async def goto(self,
                   url: str,
                   options: dict = None,
                   **kwargs: Any) -> Optional[Response]:
        """Go to the ``url``.

        :arg string url: URL to navigate page to. The url should include
            scheme, e.g. ``https://``.

        Available options are:

        * ``timeout`` (int): Maximum navigation time in milliseconds, defaults
          to 30 seconds, pass ``0`` to desable timeout. The default value can
          be changed by using the :meth:`setDefaultNavigationTimeout` method.
        * ``waitUntil`` (str|List[str]): When to consider navigation succeeded,
          defaults to ``load``. Given a list of event strings, navigation is
          considered to be successful after all events have been fired. Events
          can be either:

          * ``load``: when ``load`` event is fired.
          * ``documentloaded``: when the ``DOMContentLoaded`` event is fired.
          * ``networkidle0``: when there are no more than 0 network connections
            for at least 500 ms.
          * ``networkidle0``: when there are no more than 2 network connections
            for at least 500 ms.
        """
        options = merge_dict(options, kwargs)
        referrer = self._networkManager.extraHTTPHeaders().get('referer', '')
        requests: Dict[str, Request] = dict()
        eventListeners = [
            helper.addEventListener(
                self._networkManager, NetworkManager.Events.Request,
                lambda request: requests.__setitem__(request.url, request))
        ]

        mainFrame = self._frameManager.mainFrame
        if mainFrame is None:
            raise PageError('No main frame.')
        timeout = options.get('timeout', self._defaultNavigationTimeout)
        watcher = NavigatorWatcher(self._frameManager, mainFrame, timeout,
                                   options)

        result = await self._navigate(url, referrer)
        if result is not None:
            raise PageError(result)
        result = await watcher.navigationPromise()
        watcher.cancel()
        helper.removeEventListeners(eventListeners)
        error = result[0].pop().exception()  # type: ignore
        if error:
            raise error

        request = requests.get(mainFrame.url)
        return request.response if request else None
Example #5
0
    async def goto(self, url: str, options: dict = None, **kwargs: Any
                   ) -> Optional[Response]:
        """Go to the ``url``.

        :arg string url: URL to navigate page to. The url should include
            scheme, e.g. ``https://``.

        Available options are:

        * ``timeout`` (int): Maximum navigation time in milliseconds, defaults
          to 30 seconds, pass ``0`` to desable timeout. The default value can
          be changed by using the :meth:`setDefaultNavigationTimeout` method.
        * ``waitUntil`` (str|List[str]): When to consider navigation succeeded,
          defaults to ``load``. Given a list of event strings, navigation is
          considered to be successful after all events have been fired. Events
          can be either:

          * ``load``: when ``load`` event is fired.
          * ``documentloaded``: when the ``DOMContentLoaded`` event is fired.
          * ``networkidle0``: when there are no more than 0 network connections
            for at least 500 ms.
          * ``networkidle2``: when there are no more than 2 network connections
            for at least 500 ms.
        """
        options = merge_dict(options, kwargs)
        referrer = self._networkManager.extraHTTPHeaders().get('referer', '')
        requests: Dict[str, Request] = dict()
        eventListeners = [helper.addEventListener(
            self._networkManager, NetworkManager.Events.Request,
            lambda request: requests.__setitem__(request.url, request)
        )]

        mainFrame = self._frameManager.mainFrame
        if mainFrame is None:
            raise PageError('No main frame.')
        timeout = options.get('timeout', self._defaultNavigationTimeout)
        watcher = NavigatorWatcher(self._frameManager, mainFrame, timeout,
                                   options)

        result = await self._navigate(url, referrer)
        if result is not None:
            raise PageError(result)
        result = await watcher.navigationPromise()
        watcher.cancel()
        helper.removeEventListeners(eventListeners)
        error = result[0].pop().exception()  # type: ignore
        if error:
            raise error

        request = requests.get(mainFrame.url)
        return request.response if request else None
Example #6
0
    async def waitForNavigation(self,
                                options: dict = None,
                                **kwargs: Any) -> Optional[Response]:
        """Wait for navigation.

        Available options are same as :meth:`goto` method.
        """
        options = merge_dict(options, kwargs)
        mainFrame = self._frameManager.mainFrame
        if mainFrame is None:
            raise PageError('No main frame.')
        timeout = options.get('timeout', self._defaultNavigationTimeout)
        watcher = NavigatorWatcher(self._frameManager, mainFrame, timeout,
                                   options)
        responses: Dict[str, Response] = dict()
        listener = helper.addEventListener(
            self._networkManager, NetworkManager.Events.Response,
            lambda response: responses.__setitem__(response.url, response))
        result = await watcher.navigationPromise()
        helper.removeEventListeners([listener])
        error = result[0].pop().exception()
        if error:
            raise error

        response = responses.get(self.url, None)
        return response
Example #7
0
    async def wait_for_navigation(self, options):
        watcher = NavigatorWatcher(self._client, self.ignore_https_errors,
                                   options)
        responses = {}

        def set_resp(response):
            global responses
            responses[response['url']] = response

        listener = Helper.add_event_listener(self._network_manager,
                                             NetworkManager.Events['Response'],
                                             set_resp)
        fut = await watcher.wait_for_navigation()
        Helper.remove_event_listeners([listener])
        return responses.get(self.main_frame().url(), None)
Example #8
0
 async def waitForNavigation(self, options: dict = None, **kwargs: Any
                             ) -> Optional[Response]:
     """Wait navigation completes."""
     if options is None:
         options = dict()
     options.update(kwargs)
     watcher = NavigatorWatcher(self._client, self._ignoreHTTPSErrors,
                                options)
     responses: Dict[str, Response] = dict()
     listener = helper.addEventListener(
         self._networkManager,
         NetworkManager.Events.Response,
         lambda response: responses.__setitem__(response.url, response)
     )
     await watcher.waitForNavigation()
     helper.removeEventListeners([listener])
     return responses.get(self.url)