在Python中获取Chrome标签页网址

时间:2018-10-06 03:39:38

标签: python google-chrome

我想获取有关我的Chrome标签的信息,例如当前标签的URL或自动获取所有URL,但是我找不到有关它的任何文档。我安装了Chrome API,但从我看到的内容来看,没有任何类似的东西。谢谢您的帮助

4 个答案:

答案 0 :(得分:4)

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

支持修改当前的标签网址,其他浏览器可以添加自己的改编(如果不可用),并且可以自定义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,基于缓存的重新采集会更快。

答案 1 :(得分:1)

您可以考虑使用 / 热键

选择


F6 -为焦点的活动标签访问您的网址

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)

有关进入documentation1documentation2

的热键的更多信息

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

答案 2 :(得分:1)

您可以通过以下方式获取当前网址(或至少在地址栏中输入的网址)

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获得相应的名称,该名称是Windows 10 SDK \ Windows Kit中C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe下的一部分。然后将鼠标悬停在地址栏上,即可找到自己的名字。

这可以在许多浏览器中使用:

  • Vivaldi:Search or enter an address
  • Firefox:Search with Google or enter address
  • 歌剧:Address field
  • Chrome浏览器:Address and search bar

答案 3 :(得分:0)

有人建议使用pyautogui进行复制/粘贴,但是我不想与硬件进行交互,像我之前所说的那样更自动化的东西就是我要寻找的东西