Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_query_objects_disposed(self):
await self.page.goto(self.url + 'empty')
prototypeHandle = await self.page.evaluateHandle(
'() => HTMLBodyElement.prototype'
)
await prototypeHandle.dispose()
with self.assertRaises(ElementHandleError):
await self.page.queryObjects(prototypeHandle)
async def test_Jeval_with_missing_selector(self):
htmlContent = '<div class="a">not-a-child-div</div><div id="myId"></div>' # noqa: E501
await self.page.setContent(htmlContent)
elementHandle = await self.page.J('#myId')
with self.assertRaises(ElementHandleError) as cm:
await elementHandle.Jeval('.a', 'node => node.innerText')
self.assertIn('Error: failed to find element matching selector ".a"',
cm.exception.args[0])
async def test_wait_for_selector_fail(self):
await self.page.evaluate('() => document.querySelector = null')
with self.assertRaises(ElementHandleError):
await self.page.waitForSelector('*')
async def test_csp(self):
await self.page.evaluateOnNewDocument('() => window.injected = 123')
await self.page.goto(self.url + 'csp')
self.assertEqual(await self.page.evaluate('window.injected'), 123)
with self.assertRaises(ElementHandleError):
await self.page.addScriptTag(content='window.e = 10;')
self.assertIsNone(await self.page.evaluate('window.e'))
def executionContextById(self, contextId: str) -> ExecutionContext:
"""Get stored ``ExecutionContext`` by ``id``."""
context = self._contextIdToContext.get(contextId)
if not context:
raise ElementHandleError(
f'INTERNAL ERROR: missing context with id = {contextId}'
)
return context
async def __get_contacts_matched_with_query(chats_groups_messages_elements_list: list):
contacts_to_choose_from = [] # type : list[str , int]
get_contact_node_title_function = 'node => node.parentNode.getAttribute("title")'
for idx, element in enumerate(chats_groups_messages_elements_list):
try:
contact_name = await element.querySelectorEval(whatsapp_selectors_dict['contact_element'], get_contact_node_title_function)
if contact_name is not None:
contacts_to_choose_from.append((contact_name, idx))
except ElementHandleError:
# if it is not a contact element, move to the next one
continue
except Exception as e:
print(e)
return contacts_to_choose_from
async def evaluate(self, pageFunction: str, *args: Any,
force_expr: bool = False) -> Any:
"""Evaluate pageFunction on this frame.
Details see :meth:`pyppeteer.page.Page.evaluate`.
"""
context = await self.executionContext()
if context is None:
raise ElementHandleError('ExecutionContext is None.')
return await context.evaluate(
pageFunction, *args, force_expr=force_expr)
def valueFromRemoteObject(remoteObject: Dict) -> Any:
"""Serialize value of remote object."""
if remoteObject.get('objectId'):
raise ElementHandleError('Cannot extract value when objectId is given')
value = remoteObject.get('unserializableValue')
if value:
if value == '-0':
return -0
elif value == 'NaN':
return None
elif value == 'Infinity':
return math.inf
elif value == '-Infinity':
return -math.inf
else:
raise ElementHandleError(
'Unsupported unserializable value: {}'.format(value))
return remoteObject.get('value')
def _convertArgument(self, arg: Any) -> Dict: # noqa: C901
if arg == math.inf:
return {'unserializableValue': 'Infinity'}
if arg == -math.inf:
return {'unserializableValue': '-Infinity'}
objectHandle = arg if isinstance(arg, JSHandle) else None
if objectHandle:
if objectHandle._context != self:
raise ElementHandleError('JSHandles can be evaluated only in the context they were created!') # noqa: E501
if objectHandle._disposed:
raise ElementHandleError('JSHandle is disposed!')
if objectHandle._remoteObject.get('unserializableValue'):
return {'unserializableValue': objectHandle._remoteObject.get('unserializableValue')} # noqa: E501
if not objectHandle._remoteObject.get('objectId'):
return {'value': objectHandle._remoteObject.get('value')}
return {'objectId': objectHandle._remoteObject.get('objectId')}
return {'value': arg}