如何获取python的Internet Explorer地址栏

时间:2010-03-31 19:32:09

标签: python internet-explorer pywin32

我需要抓住互联网资源管理器地址栏。如何获取python的地址栏URL? (我需要第二部分其他浏览器抢占地址栏,但迫切需要Internet Explorer)。

感谢。

2 个答案:

答案 0 :(得分: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 :(得分:1)

这样的事情不起作用吗?

import win32gui

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