【发布时间】:2019-03-11 13:04:48
【问题描述】:
我想获取有关我的 Chrome 选项卡的信息,例如当前选项卡的 URL,或自动获取所有 URL,但我找不到任何有关它的文档。我安装了 Chrome API,但从我所看到的情况来看,没有任何类似的东西。感谢您的帮助
【问题讨论】:
标签: python google-chrome
我想获取有关我的 Chrome 选项卡的信息,例如当前选项卡的 URL,或自动获取所有 URL,但我找不到任何有关它的文档。我安装了 Chrome API,但从我所看到的情况来看,没有任何类似的东西。感谢您的帮助
【问题讨论】:
标签: python google-chrome
您可以考虑使用 keys / hotkeys。
F6 - 访问您的 URL 以获得焦点活动标签
import pyautogui
pyautogui.click(0, 200) # a random click for focusing the browser
pyautogui.press('f6')
CTRL + TAB - 下一个标签
pyautogui.hotkey('ctrl', 'tab')
CTRL + SHIFT + TAB - 上一个标签
pyautogui.hotkey('ctrl', 'shift', 'tab')
ALT + TAB - 用于另一个 chrome 窗口(如果您知道自己只打开了 chrome,则很有用)
pyautogui.hotkey('alt', 'tab')
CTRL + T - 打开新标签
pyautogui.hotkey('ctrl', 't')
CTRL + W - 关闭当前标签
pyautogui.hotkey('ctrl', 'w')
对于复制网址,您可以使用任一pyperclip...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import pyperclip # pip install pyperclip required
url = pyperclip.paste()
print(url)
任一剪贴板模块...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import clipboard # pip install clipboard required
url = clipboard.paste()
print(url)
更多关于热键的信息:documentation1, documentation2
PS:我在 Windows 64 位操作系统上安装了 Chrome x78.0,它对我有用。 :)
【讨论】:
您可以通过
获取当前 URL(或至少在地址栏中输入的内容)from pywinauto import Application
app = Application(backend='uia')
app.connect(title_re=".*Chrome.*")
element_name="Address and search bar"
dlg = app.top_window()
url = dlg.child_window(title=element_name, control_type="Edit").get_value()
print(url)
如果您使用其他语言,element_name 可能会有所不同。您可以通过inspect.exe 获取相应的名称,它是C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe 下Windows 10 SDK\Windows Kit 的一部分。然后只需将鼠标悬停在地址栏上即可找到您的姓名。
顺便说一句,这适用于许多浏览器:
Search or enter an address
Search with Google or enter address
Address field
Address and search bar
【讨论】:
不用担心本地语言解决方案和对 Chrome、Firefox、Edge、Opera 和其他大多数 chromium 引擎浏览器的支持:
支持修改当前tab url,其他浏览器不可用可自行添加适配,更多UIAutomation支持的功能可自定义。
import uiautomation as auto
class BrowserWindow:
def __init__(self, browser_name, window_index=1):
"""
A Browser Window support UIAutomation.
:param browser_name: Browser name, support 'Google Chrome', 'Firefox', 'Edge', 'Opera', etc.
:param window_index: Count from back to front, default value 1 represents the most recently created window.
"""
if browser_name == 'Firefox':
addr_bar = auto.Control(Depth=1, ClassName='MozillaWindowClass', foundIndex=window_index) \
.ToolBarControl(AutomationId='nav-bar').ComboBoxControl(Depth=1, foundIndex=1) \
.EditControl(Depth=1, foundIndex=1)
else:
win = auto.Control(Depth=1, ClassName='Chrome_WidgetWin_1', SubName=browser_name, foundIndex=window_index)
win_pane = win.PaneControl(Depth=1, Compare=lambda control, _depth: control.Name != '')
if browser_name == 'Edge':
addr_pane = win_pane.PaneControl(Depth=1, foundIndex=1).PaneControl(Depth=1, foundIndex=2) \
.PaneControl(Depth=1, foundIndex=1).ToolBarControl(Depth=1, foundIndex=1)
elif browser_name == 'Opera':
addr_pane = win_pane.GroupControl(Depth=1, foundIndex=1).PaneControl(Depth=1, foundIndex=1) \
.PaneControl(Depth=1, foundIndex=2).GroupControl(Depth=1, foundIndex=1) \
.GroupControl(Depth=1, foundIndex=1).ToolBarControl(Depth=1, foundIndex=1) \
.EditControl(Depth=1, foundIndex=1)
else:
addr_pane = win_pane.PaneControl(Depth=1, foundIndex=2).PaneControl(Depth=1, foundIndex=1) \
.PaneControl(Depth=1, Compare=lambda control, _depth:
control.GetFirstChildControl() and control.GetFirstChildControl().ControlTypeName == 'ButtonControl')
addr_bar = addr_pane.GroupControl(Depth=1, foundIndex=1).EditControl(Depth=1)
assert addr_bar is not None
self.addr_bar = addr_bar
@property
def current_tab_url(self):
"""Get current tab url."""
return self.addr_bar.GetValuePattern().Value
@current_tab_url.setter
def current_tab_url(self, value: str):
"""Set current tab url."""
self.addr_bar.GetValuePattern().SetValue(value)
browser = BrowserWindow('Google Chrome')
print(browser.current_tab_url)
browser.current_tab_url = 'www.google.com'
print(browser.current_tab_url)
pywinauto和uiautomation背后的原理都是Windows UI Automation。
Pywinauto 搜索控件对我来说太慢了,因为它需要搜索所有子树。 如果你想要更快的速度,自定义搜索位置访问UI可能会更快,uiautomation是一个包装包Python-UIAutomation-for-Windows。
以上代码测试第一次获取速度在0.1s以内,平均0.05s,基于缓存重新获取会更快。
【讨论】: