How To Get Window handles In Mozilla Firefox

1.6k views Asked by At

How to get handles of all the child windows in Mozilla firefox. Spy++ is showing only one class and window caption for all the windows. I tried using GetWindow and FindWindowEx but got nothing. I want to read URL from mozilla firefox.

2

There are 2 answers

1
IInspectable On BEST ANSWER

Spy++ uses standard Windows API calls to inspect window hierarchies (EnumWindows, EnumChildWindows, etc.). If Spy++ doesn't show any native windows, then there aren't any native windows. Consequently, you cannot find any native windows either. Firefox uses what's called Windowless Controls.

If you need to automate a GUI (which is likely the reason why you wish to find native windows), use the standard infrastructure available: UI Automation.

0
Yusuf Syam On

I got this answer from hazzey's answer and Ignas2526's comment on this post here

import win32gui
import win32ui
from ctypes import windll
from PIL import Image
import numpy as np

tab_name= '''[4k][60FPS] Optimus prime vs Lockdown Final Fight 4K 60FPS HFR[UHD] ULTRA HD - YouTube — Mozilla Firefox'''
hwnd= win32gui.FindWindow(None, tab_name)

window_rect = win32gui.GetWindowRect(hwnd)
w = window_rect[2] - window_rect[0]
h = window_rect[3] - window_rect[1]

hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 2)

# print(result)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

im = Image.frombuffer(
    'RGB',
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
    bmpstr, 'raw', 'BGRX', 0, 1)
im.shape = (h, w, 4)
# im= np.array(im)

win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)

if result == 1:
    # PrintWindow Succeeded
    im.save("test.png")

For mozilla (or at least for my computer) the third parameter for this statement is 2 to make it work, but if not you can play and try another number with it.

result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 2)

The test.png: The test.png