如何获取Firefox地址栏URL用于Python(pywin32)

1 投票
1 回答
3263 浏览
提问于 2025-04-15 21:22

我想获取Firefox浏览器的地址栏内容。请问如何用Python获取地址栏的URL?(我还需要获取其他浏览器,比如Chrome和Safari的地址栏内容,但现在最急需的是Firefox的)。

谢谢。

相关问题:

1 个回答

3

你需要查看所有的顶层窗口,看看它们的标题里是否包含“firefox”,或者使用spy++检查firefox的窗口类别,然后再查看所有子窗口以找到网址。作为一个起点,可以尝试这样做:

import win32gui

def enumerationCallaback(hwnd, results):
    text = win32gui.GetWindowText(hwnd)
    if text.find("Mozilla Firefox") >= 0:
        results.append((hwnd, text))

mywindows = []    
win32gui.EnumWindows(enumerationCallaback, mywindows)
for win, text in mywindows:
    print text

def recurseChildWindow(hwnd, results):
    win32gui.EnumChildWindows(hwnd, recurseChildWindow, results)
    print hwnd
    # try to get window class, text etc using SendMessage and see if it is what we want

mychildren = []
recurseChildWindow(mywindows[0][0], mychildren)

另外,你可以使用这个模块来完成大部分这样的任务,链接在这里:http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html

撰写回答