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

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

问题描述

我想获取有关我的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

推荐答案

不用担心本地语言解决方案以及对Edge和其他Chromium引擎浏览器的支持:

Don't worry about local-language solutions and support for Edge and other Chromium engine browsers:

import uiautomation as auto


def get_browser_tab_url(browser: str):
    """
    Get browser tab url, browser must already open
    :param browser: Support 'Edge' 'Google Chrome' and other Chromium engine browsers
    :return: Current tab url
    """
    if browser.lower() == 'edge':
        addr_bar = auto.EditControl(AutomationId='addressEditBox')
    else:
        win = auto.PaneControl(Depth=1, ClassName='Chrome_WidgetWin_1', SubName=browser)
        temp = win.PaneControl(Depth=1, Name=browser).GetChildren()[1].GetChildren()[0]
        for bar in temp.GetChildren():
            last = bar.GetLastChildControl()
            if last and last.Name != '':
                break
        addr_bar = bar.GroupControl(Depth=1, Name='').EditControl()
    url = addr_bar.GetValuePattern().Value
    return url


print(get_browser_tab_url('Edge'))
print(get_browser_tab_url('Google Chrome'))
print(get_browser_tab_url('Cent Browser'))

pywinauto和uiautomation背后的原理都是 Windows UI自动化.

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.

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

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