Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _handleException(self, exceptionDetails: Dict) -> None:
message = helper.getExceptionMessage(exceptionDetails)
self.emit(Page.Events.PageError, PageError(message))
async def queryObjects(self, prototypeHandle: JSHandle) -> JSHandle:
"""Iterate js heap and finds all the objects with the handle.
:arg JSHandle prototypeHandle: JSHandle of prototype object.
"""
if not self.mainFrame:
raise PageError('no main frame.')
context = await self.mainFrame.executionContext()
if not context:
raise PageError('No context.')
return await context.queryObjects(prototypeHandle)
def url(self) -> str:
"""Get URL of this page."""
frame = self.mainFrame
if not frame:
raise PageError('no main frame.')
return frame.url
async def fetch(self, url, **kwargs):
try:
max_tries = kwargs.pop('max_tries')
except KeyError:
# default 3
max_tries = 3
# Create a new page to load url.
page = await self.browser.newPage()
try:
logger.info("try to get {url} by browser.".format(url=url))
await page.goto(url, **kwargs)
# 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:
def _onFrameNavigated(self, framePayload: dict) -> None:
isMainFrame = not framePayload.get('parentId')
if isMainFrame:
frame = self._mainFrame
else:
frame = self._frames.get(framePayload.get('id', ''))
if not (isMainFrame or frame):
raise PageError('We either navigate top level or have old version '
'of the navigated frame')
# Detach all child frames first.
if frame:
for child in frame.childFrames:
self._removeFramesRecursively(child)
# Update or create main frame.
_id = framePayload.get('id', '')
if isMainFrame:
if frame:
# Update frame id to retain frame identity on cross-process navigation. # noqa: E501
self._frames.pop(frame._id, None)
frame._id = _id
else:
# Initial main frame navigation.
async def rerun(self) -> None: # noqa: C901
"""Start polling."""
runCount = self._runCount = self._runCount + 1
success: Optional[JSHandle] = None
error = None
try:
context = await self._frame.executionContext()
if context is None:
raise PageError('No execution context.')
success = await context.evaluateHandle(
waitForPredicatePageFunction,
self._predicateBody,
self._polling,
self._timeout,
*self._args,
)
except Exception as e:
error = e
if self.promise.done():
return
if self._terminated or runCount != self._runCount:
if success:
await success.dispose()
async def type(self, selector: str, text: str, options: dict = None,
**kwargs: Any) -> None:
"""Type ``text`` on the element which matches ``selector``.
Details see :meth:`pyppeteer.page.Page.type`.
"""
options = merge_dict(options, kwargs)
handle = await self.querySelector(selector)
if handle is None:
raise PageError('Cannot find {} on this page'.format(selector))
await handle.type(text, options)
await handle.dispose()
* the ``timeout`` is exceeded during navigation
* then main resource failed to load
.. note::
:meth:`goto` either raise error or return a main resource response.
The only exceptions are navigation to ``about:blank`` or navigation
to the same URL with a different hash, which would succeed and
return ``None``.
.. note::
Headless mode doesn't support navigation to a PDF document.
"""
options = merge_dict(options, kwargs)
mainFrame = self._frameManager.mainFrame
if mainFrame is None:
raise PageError('No main frame.')
referrer = self._networkManager.extraHTTPHeaders().get('referer', '')
requests: Dict[str, Request] = dict()
def set_request(req: Request) -> None:
if req.url not in requests:
requests[req.url] = req
eventListeners = [helper.addEventListener(
self._networkManager,
NetworkManager.Events.Request,
set_request,
)]
timeout = options.get('timeout', self._defaultNavigationTimeout)
watcher = NavigatorWatcher(self._frameManager, mainFrame, timeout,
if req.url not in requests:
requests[req.url] = req
eventListeners = [helper.addEventListener(
self._networkManager,
NetworkManager.Events.Request,
set_request,
)]
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._navigationURL)
return request.response if request else None