HyperionGray / trio-chrome-devtools-protocol

Trio driver for Chrome DevTools Protocol (CDP)
MIT License
60 stars 17 forks source link

get_title.py fails --- TypeError: object _AsyncGeneratorContextManager can't be used in 'await' expression #2

Closed jean closed 4 years ago

jean commented 4 years ago

I was trying the get_title.py example, but it fails with a TypeError:

~/git/python-chrome-devtools-protocol(master)$ python3 test.py ws://127.0.0.1:9900/devtools/browser/618a25d7-... https://example.com
INFO:screenshot:Connecting to browser: ws://127.0.0.1:9900/devtools/browser/618a25d7-...
INFO:screenshot:Listing targets
INFO:screenshot:Attaching to target id=5587641B1405557....
INFO:screenshot:Navigating to https://example.com
Traceback (most recent call last):
  File "test.py", line 43, in <module>
    trio.run(main, restrict_keyboard_interrupt_to_checkpoints=True)
  File "/data/data/com.termux/files/home/.local/lib/python3.8/site-packages/trio/_core/_run.py", line 1804, in run
    raise runner.main_task_outcome.error
  File "test.py", line 29, in main
    event = await session.wait_for(page.LoadEventFired)
TypeError: object _AsyncGeneratorContextManager can't be used in 'await' expression
mehaase commented 4 years ago

Thanks for filing this! We modified the way that events are handled and neglected to update the example scripts. In the version of the script that you ran, the event listener starts after navigation begins, which causes a race condition. If the page load fires before the event listener starts (e.g. if the page is served from local cache) then the listener will hang forever.

The new approach is to register the event listener first using an async with block, then to perform the action that will trigger that event:

async with session.wait_for(page.LoadEventFired):
    await session.execute(page.navigate(target_url))

I have updated both example scripts accordingly and retested to make sure they work.