如何获取Windows中有效的Google Chrome选项卡的网址? [英] How do I get the URL of the active Google Chrome tab in Windows?

查看:543
本文介绍了如何获取Windows中有效的Google Chrome选项卡的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python脚本如何在Windows中获取当前有效的Google Chrome选项卡的网址?这必须在不中断用户的情况下完成,因此发送键盘复制/粘贴不是一种选择。

首先,您需要下载并安装 pywin32 。在脚本中导入这些模块:

  import win32gui 
import win32con



如果Google Chrome是当前活动的窗口,请先通过以下方式获取窗口句柄:



<$ p $ (code> hwnd = win32gui.GetForegroundWindow()

(否则,找到Google Chrome窗口句柄通过使用 win32gui.FindWindow Windows Detective 在查找windows的类名时很方便。)



似乎获取网址的唯一方法是获取多功能框(地址栏)。这通常是选项卡的网址,但也可以是用户当前正在输入的任何部分网址或搜索字符串。



此外,多功能框中的网址不会包含除非用户已经明确地键入(并且还没有按下输入),否则它将包括https://或ftp://,如果使用这些协议,则为http:// p>

因此,我们在当前的Chrome窗口中找到了多功能框子窗口:

  omniboxHwnd = win32gui.FindWindowEx(hwnd,0,'Chrome_OmniboxView',None)

这当然如果谷歌浏览器团队决定重新命名他们的窗口类,那就打破它。

然后我们得到多功能框的窗口文本,它似乎不适用于< a href =http://docs.activestate.com/activepython/2.5/pywin32/win32gui__GetWindowText_meth.html> win32gui.GetWindowText 。好东西有一个替代方案可以工作:

$ p $ def getWindowText(hwnd):
buf_size = 1 + win32gui.SendMessage (hwnd,win32con.WM_GETTEXTLENGTH,0,0)
buf = win32gui.PyMakeBuffer(buf_size)
win32gui.SendMessage(hwnd,win32con.WM_GETTEXT,buf_size,buf)
return str(buf)

这个小函数发送 WM_GETTEXT 消息到窗口并返回窗口文本(在本例中为多功能框中的文本)。 p>

你去了哪里!


How can my Python script get the URL of the currently active Google Chrome tab in Windows? This has to be done without interrupting the user, so sending key strokes to copy/paste is not an option.

解决方案

First, you need to download and install pywin32. Import these modules in your script:

import win32gui
import win32con

If Google Chrome is the currently active window, first get the window handle by:

hwnd = win32gui.GetForegroundWindow()

(Otherwise, find the Google Chrome window handle by using win32gui.FindWindow. Windows Detective is handy when finding out class names for windows.)

It seems the only way to get the URL is to get the text in the "omnibox" (address bar). This is usually the tab's URL, but could also be any partial URL or search string that the user is currently typing.

Also, the URL in the omnibox won't include the "http://" prefix unless the user has typed it explicitly (and not yet pressed enter), but it will in fact include "https://" or "ftp://" if those protocols are used.

So, we find the omnibox child window inside the current Chrome window:

omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)

This will of course break if the Google Chrome team decides to rename their window classes.

And then we get the "window text" of the omnibox, which doesn't seem to work with win32gui.GetWindowText for me. Good thing there's an alternative that does work:

def getWindowText(hwnd):
    buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
    buf = win32gui.PyMakeBuffer(buf_size)
    win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf)
    return str(buf)

This little function sends the WM_GETTEXT message to the window and returns the window text (in this case, the text in the omnibox).

There you go!

这篇关于如何获取Windows中有效的Google Chrome选项卡的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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