在 Python 中获取 Chrome 标签 URL

Noi*_*ivy 7 python google-chrome

我想获取有关我的 Chrome 选项卡的信息,例如当前选项卡的 URL 或自动获取所有 URL,但我找不到任何有关它的文档。我安装了 Chrome API,但我所看到的没有类似的东西。谢谢你的帮助

skj*_*rns 10

您可以通过以下方式获取当前 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)
Run Code Online (Sandbox Code Playgroud)

如果您使用其他语言,情况element_name可能会有所不同。inspect.exe您可以通过 Windows 10 SDK\Windows Kit 下的获取相应的名称C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe。然后只需将鼠标悬停在地址栏上,您就会找到您的名字。

顺便说一句,这适用于许多浏览器:

  • 维瓦尔第:Search or enter an address
  • 火狐浏览器:Search with Google or enter address
  • 歌剧:Address field
  • 铬合金:Address and search bar


bru*_*uce 7

不用担心本地语言解决方案和对 Chrome、Firefox、Edge、Opera 和其他大多数 Chrome 引擎浏览器的支持:

支持修改当前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)
Run Code Online (Sandbox Code Playgroud)

pywinauto 和 uiautomation 背后的原理都是Windows UI Automation

Pywinauto 搜索控制对我来说太慢了,因为它需要搜索所有子树。如果你想要更快的速度,自定义搜索位置来访问 UI 可能会更快, uiautomation 是一个包装包Python- UIAutomation -for-Windows

上面代码测试第一次获取速度在0.1s以内,平均0.05s,基于缓存重新获取会更快。


Cri*_* F. 5

您可以考虑使用 usink/热键

选择


F6 - 访问您的 url 以获得焦点活动选项卡

import pyautogui
pyautogui.click(0, 200) # a random click for focusing the browser
pyautogui.press('f6')
Run Code Online (Sandbox Code Playgroud)

CTRL + TAB - 下一个标签

pyautogui.hotkey('ctrl', 'tab')
Run Code Online (Sandbox Code Playgroud)

CTRL + SHIFT + TAB - 上一个选项卡

pyautogui.hotkey('ctrl', 'shift', 'tab')
Run Code Online (Sandbox Code Playgroud)

ALT + TAB - 用于另一个 chrome 窗口(如果您知道只打开了 chrome,则很有用)

pyautogui.hotkey('alt', 'tab')

CTRL + T - 打开新标签

pyautogui.hotkey('ctrl', 't')

CTRL + W - 关闭当前选项卡

pyautogui.hotkey('ctrl', 'w')

复印


对于复制 url,您可以使用 pyperclip ...

pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import pyperclip # pip install pyperclip required
url = pyperclip.paste()
print(url)
Run Code Online (Sandbox Code Playgroud)

无论是剪贴板模块...

pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import clipboard # pip install clipboard required
url = clipboard.paste()
print(url)
Run Code Online (Sandbox Code Playgroud)

有关热键的更多信息:文档 1文档 2

PS:我在 Windows 64 位操作系统上有 Chrome x78.0,它对我有用。:)