async def evaluateHandle(self, pageFunction: str, *args: Any) -> 'JSHandle': """Execute `pageFunction` on this context.""" if not args and not helper.is_jsfunc(pageFunction): _obj = await self._client.send( 'Runtime.evaluate', { 'expression': pageFunction, 'contextId': self._contextId, 'returnByValue': False, 'awaitPromiss': True, }) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject) _obj = await self._client.send( 'Runtime.callFunctionOn', { 'functionDeclaration': pageFunction, 'executionContextId': self._contextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromiss': True, }) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject)
async def evaluateHandle(self, pageFunction: str, *args: Any, force_expr: bool = False) -> 'JSHandle': """Execute ``pageFunction`` on this context. Details see :meth:`pyppeteer.page.Page.evaluateHandle`. """ if force_expr or (not args and not helper.is_jsfunc(pageFunction)): _obj = await self._client.send('Runtime.evaluate', { 'expression': pageFunction, 'contextId': self._contextId, 'returnByValue': False, 'awaitPromise': True, }) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError( 'Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject) _obj = await self._client.send('Runtime.callFunctionOn', { 'functionDeclaration': pageFunction, 'executionContextId': self._contextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromise': True, }) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject)
async def evaluateHandle(self, pageFunction: str, *args: Any, force_expr: bool = False) -> 'JSHandle': 'Execute ``pageFunction`` on this context.\n\n Details see :meth:`pyppeteer.page.Page.evaluateHandle`.\n ' if (force_expr or ((not args) and (not helper.is_jsfunc(pageFunction)))): _obj = (await self._client.send( 'Runtime.evaluate', { 'expression': pageFunction, 'contextId': self._contextId, 'returnByValue': False, 'awaitPromise': True, })) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject) _obj = (await self._client.send( 'Runtime.callFunctionOn', { 'functionDeclaration': pageFunction, 'executionContextId': self._contextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromise': True, })) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject)
async def evaluateHandle(self, pageFunction: str, *args: Any, force_expr: bool = False) -> 'JSHandle': 'Execute ``pageFunction`` on this context.\n\n Details see :meth:`pyppeteer.page.Page.evaluateHandle`.\n ' suffix = ''.join( ['//# sourceURL=', '{}'.format(EVALUATION_SCRIPT_URL)]) if (force_expr or ((not args) and (not helper.is_jsfunc(pageFunction)))): try: if SOURCE_URL_REGEX.match(pageFunction): expressionWithSourceUrl = pageFunction else: expressionWithSourceUrl = ''.join( ['{}'.format(pageFunction), '\n', '{}'.format(suffix)]) _obj = (await self._client.send( 'Runtime.evaluate', { 'expression': expressionWithSourceUrl, 'contextId': self._contextId, 'returnByValue': False, 'awaitPromise': True, 'userGesture': True, })) except Exception as e: _rewriteError(e) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject) try: _obj = (await self._client.send( 'Runtime.callFunctionOn', { 'functionDeclaration': ''.join([ '{}'.format(pageFunction), '\n', '{}'.format(suffix), '\n' ]), 'executionContextId': self._contextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromise': True, 'userGesture': True, })) except Exception as e: _rewriteError(e) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return self._objectHandleFactory(remoteObject)
async def evaluateHandle( self, pageFunction: str, *args: Any, # noqa: C901 force_expr: bool = False) -> 'JSHandle': """Execute ``pageFunction`` on this context. Details see :meth:`pyppeteer.page.Page.evaluateHandle`. """ suffix = f'//# sourceURL={EVALUATION_SCRIPT_URL}' if force_expr or (not args and not helper.is_jsfunc(pageFunction)): try: if SOURCE_URL_REGEX.match(pageFunction): expressionWithSourceUrl = pageFunction else: expressionWithSourceUrl = f'{pageFunction}\n{suffix}' _obj = await self._client.send( 'Runtime.evaluate', { 'expression': expressionWithSourceUrl, 'contextId': self._contextId, 'returnByValue': False, 'awaitPromise': True, 'userGesture': True, }) except Exception as e: _rewriteError(e) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return createJSHandle(self, remoteObject) try: _obj = await self._client.send( 'Runtime.callFunctionOn', { 'functionDeclaration': f'{pageFunction}\n{suffix}\n', 'executionContextId': self._contextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromise': True, 'userGesture': True, }) except Exception as e: _rewriteError(e) exceptionDetails = _obj.get('exceptionDetails') if exceptionDetails: raise ElementHandleError('Evaluation failed: {}'.format( helper.getExceptionMessage(exceptionDetails))) remoteObject = _obj.get('result') return createJSHandle(self, remoteObject)
async def _rawEvaluate(self, pageFunction: str, *args: Any) -> dict: if not args: expression = helper.evaluationString(pageFunction, *args) contextId = self._defaultContextId obj = await self._client.send( 'Runtime.evaluate', { 'expression': expression, 'contextId': contextId, 'returnByValue': False, 'awaitPromise': True, }) else: obj = await self._client.send( 'Runtime.callFunctionOn', { 'functionDeclaration': pageFunction, 'executionContextId': self._defaultContextId, 'arguments': [self._convertArgument(arg) for arg in args], 'returnByValue': False, 'awaitPromise': True }) exceptionDetails = obj.get('exceptionDetails', dict()) remoteObject = obj.get('result', dict()) if exceptionDetails: raise BrowserError('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails) + f'\npageFunction:\n{pageFunction}') return remoteObject
async def evaluate(self, pageFunction: str, *args: Any) -> Any: """[Deprecated] Evaluate the pageFunction on browser.""" deprecation_msg = ( 'ElementHandle.evaluate is dropped in puppeteer. ' 'Use Page.evaluate(..., ElementHandle) instead.' ) logger.warning('[DEPRECATED] ' + deprecation_msg) warnings.warn(deprecation_msg, DeprecationWarning) if self._disposed: raise ElementHandleError('ElementHandle is disposed!') _args = ['this'] _args.extend(json.dumps(x) for x in args) stringifiedArgs = ','.join(_args) functionDeclaration = f''' function() {{ return ({pageFunction})({stringifiedArgs}) }} ''' objectId = self._remoteObject.get('objectId') obj = await self._client.send( 'Runtime.callFunctionOn', { 'objectId': objectId, 'functionDeclaration': functionDeclaration, 'returnByValue': False, 'awaitPromise': True, } ) exceptionDetails = obj.get('exceptionDetails', dict()) remoteObject = obj.get('result', dict()) if exceptionDetails: raise BrowserError( 'Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails) ) return await helper.serializeRemoteObject(self._client, remoteObject)
async def _rawEvaluate(self, pageFunction: str, *args: str) -> dict: expression = helper.evaluationString(pageFunction, *args) contextId = self._defaultContextId obj = await self._client.send('Runtime.evaluate', { 'expression': expression, 'contextId': contextId, 'returnByValue': False, 'awaitPromise': True, }) exceptionDetails = obj.get('exceptionDetails', dict()) remoteObject = obj.get('result', dict()) if exceptionDetails: raise BrowserError( 'Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails) + f'\npageFunction:\n{pageFunction}' ) return remoteObject
async def evaluate(self, pageFunction: str, *args: Any) -> Any: """Evaluate the pageFunction on browser.""" if self._disposed: raise ElementHandleError('ElementHandle is disposed!') _args = ['this'] _args.extend(json.dumps(x) for x in args) stringifiedArgs = ','.join(_args) functionDeclaration = f''' function() {{ return ({pageFunction})({stringifiedArgs}) }} ''' objectId = self._remoteObject.get('objectId') obj = await self._client.send( 'Runtime.callFunctionOn', { 'objectId': objectId, 'functionDeclaration': functionDeclaration, 'returnByValue': False, 'awaitPromise': True, }) exceptionDetails = obj.get('exceptionDetails', dict()) remoteObject = obj.get('result', dict()) if exceptionDetails: raise BrowserError('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)) return await helper.serializeRemoteObject(self._client, remoteObject)
def _handleException(self, exceptionDetails: Dict) -> None: message = helper.getExceptionMessage(exceptionDetails) self.emit(Page.Events.PageError, PageError(message))