Handle page creation and js rendering. Internal use for render/arender methods.
(self, *, url: str, script: str = None, scrolldown, sleep: int, wait: float, reload, content: Optional[str], timeout: Union[float, int], keep_page: bool)
| 500 | self.next_symbol.append(next_symbol) |
| 501 | |
| 502 | async def _async_render(self, *, url: str, script: str = None, scrolldown, sleep: int, wait: float, reload, content: Optional[str], timeout: Union[float, int], keep_page: bool): |
| 503 | """ Handle page creation and js rendering. Internal use for render/arender methods. """ |
| 504 | try: |
| 505 | page = await self.browser.newPage() |
| 506 | |
| 507 | # Wait before rendering the page, to prevent timeouts. |
| 508 | await asyncio.sleep(wait) |
| 509 | |
| 510 | # Load the given page (GET request, obviously.) |
| 511 | if reload: |
| 512 | await page.goto(url, options={'timeout': int(timeout * 1000)}) |
| 513 | else: |
| 514 | await page.goto(f'data:text/html,{self.html}', options={'timeout': int(timeout * 1000)}) |
| 515 | |
| 516 | result = None |
| 517 | if script: |
| 518 | result = await page.evaluate(script) |
| 519 | |
| 520 | if scrolldown: |
| 521 | for _ in range(scrolldown): |
| 522 | await page._keyboard.down('PageDown') |
| 523 | await asyncio.sleep(sleep) |
| 524 | else: |
| 525 | await asyncio.sleep(sleep) |
| 526 | |
| 527 | if scrolldown: |
| 528 | await page._keyboard.up('PageDown') |
| 529 | |
| 530 | # Return the content of the page, JavaScript evaluated. |
| 531 | content = await page.content() |
| 532 | if not keep_page: |
| 533 | await page.close() |
| 534 | page = None |
| 535 | return content, result, page |
| 536 | except TimeoutError: |
| 537 | await page.close() |
| 538 | page = None |
| 539 | return None |
| 540 | |
| 541 | def render(self, retries: int = 8, script: str = None, wait: float = 0.2, scrolldown=False, sleep: int = 0, reload: bool = True, timeout: Union[float, int] = 8.0, keep_page: bool = False): |
| 542 | """Reloads the response in Chromium, and replaces HTML content |