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

KeyError: 'webSocketDebuggerUrl' #5

Closed
brupelo opened this issue May 26, 2016 · 6 comments
Closed

KeyError: 'webSocketDebuggerUrl' #5

brupelo opened this issue May 26, 2016 · 6 comments

Comments

@brupelo
Copy link

brupelo commented May 26, 2016

Hi, sometimes this exception is yield:

Traceback (most recent call last):
  File "D:\sources\personal\www\www.todotrucos.info\posts\foo_post.py", line 33, in <module>
    wpp.run(filename=__file__, post_content=POST, tags=[], post_status="draft")
  File "D:\sources\personal\python\framework\web\wp_lib.py", line 280, in run
    print chrome.tabs
  File "d:\virtual_envs\py2710\lib\site-packages\chromote\__init__.py", line 122, in tabs
    return tuple(self._get_tabs())
  File "d:\virtual_envs\py2710\lib\site-packages\chromote\__init__.py", line 106, in _get_tabs
    yield ChromeTab(tab['title'], tab['url'], tab['webSocketDebuggerUrl'])
KeyError: 'webSocketDebuggerUrl'

And the code yielding that exception is very basic

chrome = Chromote()
print chrome.tabs

As i said, it happens sometimes but i don't know, any clue why?

Regards

@brupelo
Copy link
Author

brupelo commented May 26, 2016

Actually, i think you can reproduce it if you open chrome dev tools in the browser (ctrl+shift+j).

@iiSeymour
Copy link
Owner

I can reproduce this when multiple clients are attached (which isn't supported by the protocol). Are you using Chromote and the browser at the same time?

@brupelo
Copy link
Author

brupelo commented May 26, 2016

The simplest way to reproduce the issue is this:

  1. Close all opened browsers and start a single chrome browser with the remote-debugging-port parameter:

chrome.exe -remote-debugging-port=9222

  1. In that browser, open chrome dev tools (ctrl+shift+j)

  2. Run the next code:

from chromote import Chromote

chrome = Chromote()
tab = chrome.tabs[0]

At this point the exception should appear

yield ChromeTab(tab['title'], tab['url'], tab['webSocketDebuggerUrl'])

If you close the chrome dev tools and run again the snippet the exception won't be launched.

Btw, cool project of yours!

@brupelo
Copy link
Author

brupelo commented May 30, 2016

It's not real solution but locally I've patched _get_tabs:

   def _get_tabs(self):
        """
        Get all open browser tabs that are pages tabs
        """
        res = requests.get(self.url + '/json')
        for tab in res.json():
            if ('webSocketDebuggerUrl' in tab.keys()):
                if tab['type'] == 'page':
                    yield ChromeTab(tab['title'], tab['url'], tab['webSocketDebuggerUrl'])

So i can use chromote having multiple browsers where many tabs will be using the ChromeDevTools... I won't get them back of course but at least Chromote won't crash.

@iiSeymour
Copy link
Owner

Great, why not send a PR. My preference would be to drop the parens and explicate call to keys:

if 'webSocketDebuggerUrl' in tab:

@brupelo
Copy link
Author

brupelo commented May 30, 2016

Nah, your choice, it's your baby ;-)

For my use-case i've tweaked my local copy like this:

    def _get_tabs(self):
        res = requests.get(self.url + '/json')
        for tab in res.json():
            if tab['type'] == 'page':
                if ('webSocketDebuggerUrl' not in tab.keys()):
                    yield ChromeTab(tab['title'], tab['url'], None)
                else:
                    yield ChromeTab(tab['title'], tab['url'], tab['webSocketDebuggerUrl'])

and adding one method to check "valid" tabs:

    def is_valid(self):
        return self._websocket_url is not None

That way i have all available tabs from my browsers, if i want to reload one tab i just check first if is valid or not, it works wonderfully... That way i can run my scripts while other tabs have opened the chrome-dev-tools, workflow is fast.

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

2 participants