如何在Python中获取Internet Explorer地址栏
我想获取Internet Explorer的地址栏内容。请问怎么用Python获取地址栏的URL?(我需要的是获取其他浏览器地址栏的第二部分,但现在急需Internet Explorer的)。
谢谢。
2 个回答
1
这样做会不行吗?
import win32gui
def get_current_window_title():
cur_hwnd = win32gui.GetForegroundWindow()
win_title = win32gui.GetWindowText(cur_hwnd)
return win_title
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