Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _addConsoleMessage(self, type: str, args: List[JSHandle]) -> None:
if not self.listeners(Page.Events.Console):
for arg in args:
self._client._loop.create_task(arg.dispose())
return
textTokens = []
for arg in args:
remoteObject = arg._remoteObject
if remoteObject.get('objectId'):
textTokens.append(arg.toString())
else:
textTokens.append(
str(helper.valueFromRemoteObject(remoteObject)))
message = ConsoleMessage(type, ' '.join(textTokens), args)
self.emit(Page.Events.Console, message)
async def visit(browser: p_browser.Browser, url: str) -> p_page.Page:
page = await browser.newPage()
await page.setJavaScriptEnabled(False)
await page.setExtraHTTPHeaders({"DNT": "1"}) # Do Not Track (DNT)
await page.setRequestInterception(True)
@page.on("request")
async def _(request: p_network_manager.Request) -> None:
if request.resourceType in ["document", "stylesheet", "image", "font"]:
await request.continue_()
else:
await request.abort()
try:
await page.goto(url, {
# Maximum navigation time in milliseconds (* 1000):
"timeout": 5 * 1000,
async def configure_browser_and_load_whatsapp() -> (Page, Browser):
"""
Configure browser, configure the first page and open whatsapp website.
Returns:
Page -- return the first page, with whatsapp open
Browser -- return the browser object
"""
__patch_pyppeteer()
username, save_session = SessionManager.session_manager()
browser = await __config_browser(username, save_session)
pages = await get_pages(browser)
first_page = pages[0]
await config_page(first_page)
await load_website(first_page, websites['whatsapp'])
return first_page, browser
async def create(client: CDPSession, target: 'Target',
ignoreHTTPSErrors: bool, defaultViewport: Optional[Dict],
screenshotTaskQueue: list = None) -> 'Page':
"""Async function which makes new page object."""
await client.send('Page.enable'),
frameTree = (await client.send('Page.getFrameTree'))['frameTree']
page = Page(client, target, frameTree, ignoreHTTPSErrors,
screenshotTaskQueue)
await asyncio.gather(
client.send('Target.setAutoAttach', {'autoAttach': True, 'waitForDebuggerOnStart': False}), # noqa: E501
client.send('Page.setLifecycleEventsEnabled', {'enabled': True}),
client.send('Network.enable', {}),
client.send('Runtime.enable', {}),
client.send('Security.enable', {}),
client.send('Performance.enable', {}),
client.send('Log.enable', {}),
)
if ignoreHTTPSErrors:
await client.send('Security.setOverrideCertificateErrors',
{'override': True})
if defaultViewport:
await page.setViewport(defaultViewport)
async def pages(self) -> List[Page]:
"""Get all pages of this browser.
Non visible pages, such as ``"background_page"``, will not be listed
here. You can find then using :meth:`pyppeteer.target.Target.page`.
In case of multiple browser contexts, this method will return a list
with all the pages in all browser contexts.
"""
# Using asyncio.gather is better for performance
pages: List[Page] = list()
for context in self.browserContexts:
pages.extend(await context.pages())
return pages
1. Script tags inside templates are not evaluated.
2. Page styles are not visible inside templates.
""" # noqa: E501
options = merge_dict(options, kwargs)
scale = options.get('scale', 1)
displayHeaderFooter = bool(options.get('displayHeaderFooter'))
headerTemplate = options.get('headerTemplate', '')
footerTemplate = options.get('footerTemplate', '')
printBackground = bool(options.get('printBackground'))
landscape = bool(options.get('landscape'))
pageRanges = options.get('pageRanges', '')
paperWidth = 8.5
paperHeight = 11.0
if 'format' in options:
fmt = Page.PaperFormats.get(options['format'].lower())
if not fmt:
raise ValueError('Unknown paper format: ' + options['format'])
paperWidth = fmt['width']
paperHeight = fmt['height']
else:
paperWidth = convertPrintParameterToInches(options.get('width')) or paperWidth # noqa: E501
paperHeight = convertPrintParameterToInches(options.get('height')) or paperHeight # noqa: E501
marginOptions = options.get('margin', {})
marginTop = convertPrintParameterToInches(marginOptions.get('top')) or 0 # noqa: E501
marginLeft = convertPrintParameterToInches(marginOptions.get('left')) or 0 # noqa: E501
marginBottom = convertPrintParameterToInches(marginOptions.get('bottom')) or 0 # noqa: E501
marginRight = convertPrintParameterToInches(marginOptions.get('right')) or 0 # noqa: E501
preferCSSPageSize = options.get('preferCSSPageSize', False)
result = await self._client.send('Page.printToPDF', dict(
async def page(self) -> Optional[Page]:
"""Get page of this target.
If the target is not of type "page" or "background_page", return
``None``.
"""
if (self._targetInfo['type'] in ['page', 'background_page'] and
self._page is None):
client = await self._sessionFactory()
new_page = await Page.create(
client, self,
self._ignoreHTTPSErrors,
self._defaultViewport,
self._screenshotTaskQueue,
)
self._page = new_page
return new_page
def __init__(self, targetInfo: Dict, browserContext: 'BrowserContext',
sessionFactory: Callable[[], Coroutine[Any, Any, CDPSession]],
ignoreHTTPSErrors: bool, defaultViewport: Optional[Dict],
screenshotTaskQueue: List, loop: asyncio.AbstractEventLoop
) -> None:
self._targetInfo = targetInfo
self._browserContext = browserContext
self._targetId = targetInfo.get('targetId', '')
self._sessionFactory = sessionFactory
self._ignoreHTTPSErrors = ignoreHTTPSErrors
self._defaultViewport = defaultViewport
self._screenshotTaskQueue = screenshotTaskQueue
self._loop = loop
self._page: Optional[Page] = None
self._initializedPromise = self._loop.create_future()
self._isClosedPromise = self._loop.create_future()
self._isInitialized = (self._targetInfo['type'] != 'page'
or self._targetInfo['url'] != '')
if self._isInitialized:
self._initializedCallback(True)
async def stealth(page: Page) -> None:
if not isinstance(page, Page):
raise ValueError("page must is pyppeteer.page.Page")
#await chrome_runtime(page)
await console_debug(page)
await iframe_content_window(page)
#await navigator_languages(page)
await navigator_permissions(page)
await navigator_plugins(page)
await navigator_webdriver(page)
#await navigator_vendor(page)
await user_agent(page)
await webgl_vendor(page)
await window_outerdimensions(page)
await media_codecs(page)
async def craete(*args: Any, **kwargs: Any) -> Page:
"""[Deprecated] miss-spelled function.
This function is undocumented and will be removed in future release.
"""
logger.warning(
'`craete` function is deprecated and will be removed in future. '
'Use `Page.create` instead.'
)
return await Page.create(*args, **kwargs)