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

[Nodriver] Repeated KeyError from cdp.util.parse_json_event on DOM.scrollableFlagUpdated #2074

Open
michaellee94 opened this issue Nov 11, 2024 · 7 comments

Comments

@michaellee94
Copy link

michaellee94 commented Nov 11, 2024

I'm logging these exceptions from cdp.util.parse_json_event when I visit certain websites.

2024-11-10 23:51:43,649 INFO uc.connection KeyError: ('DOM.scrollableFlagUpdated',)  during parsing of json from event : {'method': 'DOM.scrollableFlagUpdated', 'params': {'nodeId': 1044, 'isScrollable': True}}
Traceback (most recent call last):
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/core/connection.py", line 649, in listener_loop
    event = cdp.util.parse_json_event(message)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/cdp/util.py", line 19, in parse_json_event
    return _event_parsers[json["method"]].from_json(json["params"])
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'DOM.scrollableFlagUpdated'
2024-11-10 23:51:43,937 INFO uc.connection KeyError: ('DOM.scrollableFlagUpdated',)  during parsing of json from event : {'method': 'DOM.scrollableFlagUpdated', 'params': {'nodeId': 1617, 'isScrollable': False}}
Traceback (most recent call last):
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/core/connection.py", line 649, in listener_loop
    event = cdp.util.parse_json_event(message)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/cdp/util.py", line 19, in parse_json_event
    return _event_parsers[json["method"]].from_json(json["params"])
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'DOM.scrollableFlagUpdated'
2024-11-10 23:51:45,216 INFO uc.connection KeyError: ('DOM.scrollableFlagUpdated',)  during parsing of json from event : {'method': 'DOM.scrollableFlagUpdated', 'params': {'nodeId': 1917, 'isScrollable': False}}
Traceback (most recent call last):
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/core/connection.py", line 649, in listener_loop
    event = cdp.util.parse_json_event(message)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/.local/lib/python3.12/site-packages/nodriver/cdp/util.py", line 19, in parse_json_event
    return _event_parsers[json["method"]].from_json(json["params"])
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^

Will attach a minimal example

@michaellee94 michaellee94 changed the title [Nodriver] Repeated KeyError from cdp.util.parse_json_event after attaching a handler for network.responseReceived events [Nodriver] Repeated KeyError from cdp.util.parse_json_event Nov 11, 2024
@michaellee94
Copy link
Author

michaellee94 commented Nov 11, 2024

This is a working example (minus the credentials):

import asyncio
import logging
import os
import sys

import nodriver as uc

ACCOUNT_ID = "foo"
PASSWORD = "bar"

USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"


async def login(driver: uc.Browser):
    tab = await driver.get("https://betanysports.eu/login", new_tab=True)

    username_field = None
    while not username_field:
        username_field = await tab.query_selector("[id='usernameDesk']")
        password_field = await tab.query_selector("[id='passwordDesk']")
        login_button = await tab.find("LOGIN")
        await asyncio.sleep(0.1)

    await username_field.send_keys(ACCOUNT_ID)
    await asyncio.sleep(0.1)
    await password_field.send_keys(PASSWORD)
    await asyncio.sleep(0.1)
    await login_button.click()

    return tab


async def test_fn(driver: uc.Browser):
    tab = await login(driver)

    fighting_tab = None
    while not fighting_tab:
        await asyncio.sleep(0.1)
        fighting_tab = await tab.query_selector("[id='img_Fighting']")
    await fighting_tab.click()
    await asyncio.sleep(0.5)
    ufc = await tab.query_selector("label[for='gl_Fighting_UFC_G']")
    await ufc.click()

    while True:
        await asyncio.sleep(1)


def handle_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return

    logger.error(
        "Uncaught exception",
        exc_info=(exc_type, exc_value, exc_traceback),
    )


async def main():
    driver = await uc.start(
        browser_args=[
            "--headless=new",
            f"--user-agent={USER_AGENT}",
            "--window-size=1920x1080",
        ]
    )
    await test_fn(driver)
    driver.stop()


if __name__ == "__main__":
    logging.basicConfig(
        filename="nodriver_bug.log",
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(name)s %(message)s",
    )
    logger = logging.getLogger(__name__)
    sys.excepthook = handle_exception

    uc.loop().run_until_complete(main())

@michaellee94
Copy link
Author

michaellee94 commented Nov 11, 2024

Here is a fix:

michaellee94/nodriver@602a3ed

I can't open a PR on https://github.com/ultrafunkamsterdam/nodriver, unfortunately. If a contributor to the repo would be willing to take a look, I would appreciate it!

@michaellee94 michaellee94 changed the title [Nodriver] Repeated KeyError from cdp.util.parse_json_event [Nodriver] Repeated KeyError from cdp.util.parse_json_event on DOM.scrollableFlagUpdated Nov 11, 2024
@therealpurplemana
Copy link

Here is a fix:

michaellee94/nodriver@602a3ed

I can't open a PR on https://github.com/ultrafunkamsterdam/nodriver, unfortunately. If a contributor to the repo would be willing to take a look, I would appreciate it!

Could you please PR this into here? https://github.com/stephanlensky/zendriver. This is a friendlier to PR fork including a number of fixes.

@michaellee94
Copy link
Author

@therealpurplemana Can do!

@therealpurplemana
Copy link

@therealpurplemana Can do!

I think it's already fixed. I tried to PR it myself but didn't see the DomScrollable code.

@michaellee94
Copy link
Author

@therealpurplemana Can do!

I think it's already fixed. I tried to PR it myself but didn't see the DomScrollable code.

@therealpurplemana It's not yet implemented in zendriver, so I opened a PR here.

@mdmintz
Copy link

mdmintz commented Nov 12, 2024

Here is a fix:

michaellee94/nodriver@602a3ed

I can't open a PR on https://github.com/ultrafunkamsterdam/nodriver, unfortunately. If a contributor to the repo would be willing to take a look, I would appreciate it!

Thank you. I added that to MyCDP, which is used by my nodriver fork ("CDP Mode") in https://github.com/seleniumbase/SeleniumBase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants