以编程方式打开的浏览器选项卡列表

以编程方式打开的浏览器选项卡列表

本文介绍了以编程方式打开的浏览器选项卡列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过索引以编程方式获取浏览器中打开的标签列表?

例如,假设 Google Chrome 使用两个标签页打开.
在程序中,一行类似于:

tabs_list = list_tabs(hwnd)

...其中 hwnd 是整个 Chrome 实例的窗口句柄,tabs_list 是一个类似于以下内容的字典:

[0 : 'http://stackoverflow.com/',1 : 'http://www.coolstuffff.com/']

(...或者可能是窗口标题而不是 url)

如果是这样,那么可以使用键盘命令从 Python 脚本中将焦点集中到其中一个,control- (CTRL-) 像 control-1 或 control-2.

稍后添加的一个编辑试图帮助澄清:想象一个本地 wxPython 应用程序,您已经知道如何从本地运行的 wxPython 应用程序中的同一个框上激活给定的 Chrome 实例,并且该浏览器实例具有多个选项卡打开,现在您要确保某个选项卡具有焦点,以便能够与正在显示的网页进行交互(例如,可以使用 CTRL-A CTRL-C 来获取其内容).这个问题不是关于发出键盘命令,这是已知的,问题是如何获取打开的标签列表,如果可能,谢谢.

解决方案

虽然不确定您的目标操作系统,但您可以在 Mac OS X 上执行此操作:

>>>从应用脚本导入 *>>>地图(lambda x: x.title(), app('Google Chrome').windows[0].tabs())[u'Stack Overflow', u'Google']

在 Windows 上,您将需要使用 Python 研究 OLE 自动化.

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.

解决方案

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.

这篇关于以编程方式打开的浏览器选项卡列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:05