Capturing console logs #260
-
I'm trying to capture console log events from pydoll but it does not seem to work. import asyncio
from functools import partial
from pydoll.browser import Chrome
from pydoll.browser.tab import Tab
from pydoll.protocol.runtime.events import (
ConsoleAPICalledEvent,
ConsoleAPICallType,
)
async def main():
async with Chrome() as browser:
tab = await browser.start()
await tab.enable_runtime_events()
async def on_console_event(tab: Tab, event: ConsoleAPICalledEvent):
print(event)
await tab.on(ConsoleAPICallType.LOG, partial(on_console_event, tab))
await tab.go_to("https://www.google.com")
await tab.execute_script("console.log('Hello from pydoll!')")
await asyncio.sleep(5)
if __name__ == "__main__":
asyncio.run(main()) What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
choucavalier
Sep 3, 2025
Replies: 1 comment
-
Found the solution: import asyncio
from functools import partial
from pydoll.browser import Chrome
from pydoll.browser.tab import Tab
from pydoll.protocol.runtime.events import (
ConsoleAPICalledEvent,
ConsoleAPICallType,
RuntimeEvent,
)
async def main():
async with Chrome() as browser:
tab = await browser.start()
await tab.enable_runtime_events()
async def on_console_event(event: ConsoleAPICalledEvent):
# Filter for log events specifically if needed
if event['params']['type'] == ConsoleAPICallType.LOG:
print(f"Console log: {event['params']['args'][0]['value'] if event['params']['args'] else 'No args'}")
print(f"Full event: {event}")
await tab.on(RuntimeEvent.CONSOLE_API_CALLED, on_console_event)
await tab.go_to("https://www.google.com")
await tab.execute_script("console.log('Hello from pydoll!')")
await asyncio.sleep(5)
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
choucavalier
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found the solution: