3

I have the following piece of code which loads a page and follows a link within it, using asyncio.gather as recommended in the documentation for the click method:

import asyncio

import pyppeteer


async def main(selector):
    browser = await pyppeteer.launch()
    page = await browser.newPage()
    await page.goto("https://example.org")
    result = await asyncio.gather(
        page.waitForNavigation(),
        page.click(selector),
    )
    result = next(filter(None, result))
    if result and isinstance(result, pyppeteer.network_manager.Response):
        print(result.status, result.url)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(selector="a"))

This works wonders when the click method does trigger a navigation event:

200 https://www.iana.org/domains/reserved

But I'm trying to write generic code that could handle any selector, even those which would not cause a navigation. If I change the selector to "h1" I get the following timeout, because there is no navigation to wait for:

Traceback (most recent call last):
  File "stackoverflow.py", line 20, in <module>
    loop.run_until_complete(main(selector="h1"))
  File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "stackoverflow.py", line 12, in main
    page.click(selector),
  File "/.../lib/python3.6/site-packages/pyppeteer/page.py", line 938, in waitForNavigation
    raise error
pyppeteer.errors.TimeoutError: Navigation Timeout Exceeded: 30000 ms exceeded.

I could not find a way to detect whether or not the click event would produce an additional request, in order to avoid the Page.waitForNavigation call. Is it possible? Thanks in advance for your time!

elacuesta
  • 891
  • 5
  • 20

0 Answers0