2010-03-31 47 views

回答

3

以下適用於我。

from win32com.client import Dispatch 

SHELL = Dispatch("Shell.Application") 

def get_ie(shell): 
    for win in shell.Windows(): 
     if win.Name == "Windows Internet Explorer": 
      return win 
    return None 

def main(): 
    ie = get_ie(SHELL) 
    if ie: 
     print ie.LocationURL 
    else: 
     print "no ie window" 

if __name__ == '__main__': 
    main() 

我試圖用Dispatch('InternetExplorer.Application')連接到當前IE窗口,但它總是會創建一個新窗口。代碼會更簡單。

# This doesn't work 
from win32com.client import Dispatch 

# This line will always create a new window 
ie = Dispatch("InternetExplorer.Application") 

print ie.LocationURL 
1

會這樣的東西不行嗎?

import win32gui 

def get_current_window_title(): 
    cur_hwnd = win32gui.GetForegroundWindow() 
    win_title = win32gui.GetWindowText(cur_hwnd) 
    return win_title 
+0

感謝的人的答案。但我需要地址欄。不需要標題。 – 2010-03-31 19:46:24