How to use the pyppeteer.errors.NetworkError function in pyppeteer

To help you get started, we’ve selected a few pyppeteer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github miyakogi / pyppeteer / tests / test_page.py View on Github external
async def test_nice_error_after_navigation(self):
        executionContext = await self.page.mainFrame.executionContext()

        await asyncio.wait([
            self.page.waitForNavigation(),
            executionContext.evaluate('window.location.reload()'),
        ])

        with self.assertRaises(NetworkError) as cm:
            await executionContext.evaluate('() => null')
        self.assertIn('navigation', cm.exception.args[0])
github miyakogi / pyppeteer / tests / test_abnormal_crash.py View on Github external
async def test_browser_crash_send(self):
        browser = await launch(args=['--no-sandbox'])
        page = await browser.newPage()
        await page.goto('about:blank')
        await page.querySelector("title")
        browser.process.terminate()
        browser.process.wait()

        if current_platform().startswith('win'):
            # wait for terminating browser process
            await asyncio.sleep(1)

        with self.assertRaises(NetworkError):
            await page.querySelector("title")
        with self.assertRaises(NetworkError):
            with self.assertLogs('pyppeteer', logging.ERROR):
                await page.querySelector("title")
        with self.assertRaises(ConnectionError):
            await browser.newPage()
github miyakogi / pyppeteer / tests / test_execution_context.py View on Github external
async def test_json_circular_object_error(self):
        windowHandle = await self.page.evaluateHandle('window')
        with self.assertRaises(NetworkError) as cm:
            await windowHandle.jsonValue()
        self.assertIn('Object reference chain is too long',
                      cm.exception.args[0])
github miyakogi / pyppeteer / tests / test_frame.py View on Github external
async def test_fail_page_closed(self):
        page = await self.context.newPage()
        await page.goto(self.url + 'empty')
        task = page.waitForSelector('.box')
        await page.close()
        with self.assertRaises(NetworkError):
            await task
github miyakogi / pyppeteer / pyppeteer / network_manager.py View on Github external
def _handleRequestRedirect(self, request: 'Request', redirectStatus: int,
                               redirectHeaders: Dict, fromDiskCache: bool,
                               fromServiceWorker: bool,
                               securityDetails: Dict = None) -> None:
        response = Response(self._client, request, redirectStatus,
                            redirectHeaders, fromDiskCache, fromServiceWorker,
                            securityDetails)
        request._response = response
        request._redirectChain.append(request)
        response._bodyLoadedPromiseFulfill(
            NetworkError('Response body is unavailable for redirect response')
        )
        self._requestIdToRequest.pop(request._requestId, None)
        self._attemptedAuthentications.discard(request._interceptionId)
        self.emit(NetworkManager.Events.Response, response)
        self.emit(NetworkManager.Events.RequestFinished, request)
github miyakogi / pyppeteer / pyppeteer / network_manager.py View on Github external
interception is not enabled, raise ``NetworkError``.

        ``response`` is a dictionary which can have the following fields:

        * ``status`` (int): Response status code, defaults to 200.
        * ``headers`` (dict): Optional response headers.
        * ``contentType`` (str): If set, equals to setting ``Content-Type``
          response header.
        * ``body`` (str|bytes): Optional response body.
        """
        if self._url.startswith('data:'):
            return
        if not self._allowInterception:
            raise NetworkError('Request interception is not enabled.')
        if self._interceptionHandled:
            raise NetworkError('Request is already handled.')
        self._interceptionHandled = True

        if response.get('body') and isinstance(response['body'], str):
            responseBody: Optional[bytes] = response['body'].encode('utf-8')
        else:
            responseBody = response.get('body')

        responseHeaders = {}
        if response.get('headers'):
            for header in response['headers']:
                responseHeaders[header.lower()] = response['headers'][header]
        if response.get('contentType'):
            responseHeaders['content-type'] = response['contentType']
        if responseBody and 'content-length' not in responseHeaders:
            responseHeaders['content-length'] = len(responseBody)
github boramalper / cecibot / backend / backend / __main__.py View on Github external
>
        > The underlying problem is that there is a race condition between the event that clears execution contexts and
        > the event that adds new execution contexts.
        >
        > When you navigate to a new page, sometimes headless chrome will add the new contexts, before clearing the old
        > ones. When it goes to clear the contexts, it blows away the recently added context, causing: Cannot find
        > context with specified id undefined.
        >
        > I think the fix should come from upstream, since I think it's going to be quite difficult to handle correctly
        > in puppeteer.
        https://github.com/GoogleChrome/puppeteer/issues/1325#issuecomment-366254084

        Also: https://github.com/boramalper/cecibot/issues/2
        """
        height = await page.evaluate("document.documentElement.scrollHeight", force_expr=True)
    except pyppeteer.errors.NetworkError:
        # Even though the PDF we are creating is no longer full-height (i.e. has a height equal to the height of the
        # web-page), we can still create a PDF instead of failing.
        height = 1920

    await page.emulateMedia("screen")
    await page.pdf({
        "path": filePath,
        "width": "1080px",
        "height": str(height + 32) + "px"  # + 32 to prevent the last empty page (safety margin)
    })

    return filePath
github miyakogi / pyppeteer / pyppeteer / execution_context.py View on Github external
async def evaluate(self, pageFunction: str, *args: Any,
                       force_expr: bool = False) -> Any:
        """Execute ``pageFunction`` on this context.

        Details see :meth:`pyppeteer.page.Page.evaluate`.
        """
        handle = await self.evaluateHandle(
            pageFunction, *args, force_expr=force_expr)
        try:
            result = await handle.jsonValue()
        except NetworkError as e:
            if 'Object reference chain is too long' in e.args[0]:
                return
            if 'Object couldn\'t be returned by value' in e.args[0]:
                return
            raise
        await handle.dispose()
        return result
github HuberTRoy / Seen / seen / fetch_by_browser.py View on Github external
# break
        except TimeoutError:
            pass
        except PageError:
            return emptyBrowserResponse(url)

        # the TimeoutError not just occured when we cannot connect it.
        # for some reasons, it will also be happen when JavaScript not full load.
        # so we can also get the most page content.
        url = page.url
        for i in range(max_tries):
            try:
                text = await page.content()
                cookies = await page.cookies()
                break
            except NetworkError:
                # if timeout is too small, sometimes it will raise this error.
                try:
                    await page.reload(**kwargs)
                except TimeoutError:
                    pass
        else:
            await page.close()

            # text = 
            # means load failed.

            return emptyBrowserResponse(url)

        return BrowserResponse(url=url, text=text, cookies=cookies)
github miyakogi / pyppeteer / pyppeteer / connection.py View on Github external
if self._lastId and not self._connected:
            raise ConnectionError('Connection is closed')
        if params is None:
            params = dict()
        self._lastId += 1
        _id = self._lastId
        msg = json.dumps(dict(
            id=_id,
            method=method,
            params=params,
        ))
        logger_connection.debug(f'SEND: {msg}')
        self._loop.create_task(self._async_send(msg, _id))
        callback = self._loop.create_future()
        self._callbacks[_id] = callback
        callback.error: Exception = NetworkError()  # type: ignore
        callback.method: str = method  # type: ignore
        return callback