Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

python - List of open browser tabs programmatically

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.coolstuffff.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...