List of open browser tabs programmatically
Asked Answered
V

3

14

Is there a way to programmatically obtain a list of open tabs in a browser by index?

For example, suppose Google Chrome is open with two tabs.
In the program, a line something like:

tabs_list = list_tabs(hwnd)

...where hwnd is the handle to the window for the overall Chrome instance and tabs_list is a dictionary something like:

{
  0 : 'http://stackoverflow.com/',
  1 : 'http://www.otherstufff.com/'
}

(...or maybe by title of the window instead of url)

If so, then bringing focus to one of them can be possible from the Python script with keyboard commands, control- (CTRL-) like control-1 or control-2.


An edit added later to try to help clarify: Picture a local wxPython app, where you already know how to activate a given instance of Chrome on that same box from within your wxPython app running locally, and that browser instance has multiple tabs open, and now you want to insure that a certain tab has focus, to be able to interact with that web page that is being displayed (maybe using CTRL-A CTRL-C for example to harvest its content). This question is not about issuing keyboard commands, that is already known, the question is how to obtain a list of open tabs, if possible, thanks.

Vulvitis answered 24/9, 2011 at 8:9 Comment(2)
It will really help to know the platform (os). I'm guessing from hwnd it's Windows but adding this detail (and tagging appropriately) would be essential in giving you an answer.Doodle
Did not specify OS because the goal is to develop a cross-platform application, however, if we need to limit it, starting with Windows. Yet, do really appreciate your example for Mac OS X and the tip for OLE Automation with Python, thanks!Vulvitis
D
12

While not sure of your target OS, you can do this on Mac OS X:

>>> from appscript import *
>>> map(lambda x: x.title(), app('Google Chrome').windows[0].tabs())
[u'Stack Overflow', u'Google']

On Windows, you will want to look into OLE Automation with Python.

Doodle answered 24/9, 2011 at 8:50 Comment(0)
W
1

Its not possible. First you haven't noted what app you develop but if you use python for a website backend - then its just a server-side language and does not know what a "browser" is - the server outputs to the browser and thats all. And I don't believe it's possible with client-side language like javascript as this seem to be a serious security and privacy issue if it was possible.

Edit: If you are developing a Chrome plugin lets say it might be a whole different story - but then it goes towards the Chrome API and your tagging is wrong, as "python" itself can not do that.

Whittier answered 24/9, 2011 at 8:12 Comment(2)
Primarily Python, see note added to original question. I do not necessarily think Python cannot do that, because Python can call Windows (for example) API's via ctypes, win32api and other modules, lots-o-fun.Vulvitis
I think he was talking about a desktop app.Jeanene
M
-1

You can get the title of all open windows using the PyGetWindow library.

This won't give you a list of tabs open in your browser, but it should give you the title of the current active tab in your browser.

https://pypi.org/project/PyGetWindow/

Madrigal answered 1/4 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.