2

I'm trying to get the url of the current window using Python (in case having focus navegator), but I couldn't, it shows Error.

Could someone guide me please, i'm new in python (i'm Java developer).

import time
import win32gui
import uiautomation as auto

_active_window_name = None

while True:
    window = win32gui.GetForegroundWindow()
    chromeControl = auto.ControlFromHandle(window)
    edit = chromeControl.EditControl()

    #print(dir(chromeControl.EditControl()))
    print(edit.GetValuePattern().Value)

    time.sleep(5)

1 Answers1

0

Try out this (don't actually need win32gui):

import uiautomation as auto


control = auto.GetFocusedControl()
controlList = []
while control:
    controlList.insert(0, control)
    control = control.GetParentControl()
    
control = controlList[0 if len(controlList) == 1 else 1]
    
address_control = auto.FindControl(control, lambda c, d: 
                                            isinstance(c, auto.EditControl))

print('Current URL:')
print(address_control.GetValuePattern().Value)

you can also then set some new value for the OmniBox:

address_control.GetValuePattern().SetValue('http://some_url')
macsunmood
  • 1
  • 1
  • 1
  • 1