在 Python 中获取 Chrome 标签 URL [英] Get Chrome tab URL in Python

查看:27
本文介绍了在 Python 中获取 Chrome 标签 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get information about my Chrome tabs like the URL of the current tab or get all URLs automatically but I can't find any documentation about it. I installed the Chrome API but there's nothing like that from what I have seen. Thanks for your help

推荐答案

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

Don't worry about local-language solutions and support for Chrome, Firefox, Edge, Opera and other most chromium engine browsers:

支持修改当前tab url,其他浏览器没有的可以自行适配,更多功能UIAutomation 可以自定义.

Support to modify current tab url, other browsers can add their own adaptations if they are not available, and more functions supported by UIAutomation can be customized.

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用户界面自动化.

The principle behind pywinauto and uiautomation is both Windows UI Automation.

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

Pywinauto search control was too slow for me because it needed to search all the subtrees. If you want faster speed, customizing the search location to access the UI may be faster, uiautomation is a wrapper package Python-UIAutomation-for-Windows.

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

The above code test first acquisition speed is within 0.1s, average 0.05s, re-acquisition based on cache will be faster.

这篇关于在 Python 中获取 Chrome 标签 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆