Internet Explorer COM failing sporadically

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CotswoldsMaker
Posts: 12
Joined: 16 Dec 2020, 04:42

Internet Explorer COM failing sporadically

16 Dec 2020, 04:51

Hi all,

I have been using AHK for over 9 months now and love it. It has helped so much in my work flow as a medical doctor. Every second counts now that COVID is slowing down our work flow.

I have been working on controlling web based programs via the internet explorer COMs object. This works very well, but I am having a sporadic problem where the program will just crash with the error message (or similar) as below:

error message.jpg
Error message
error message.jpg (53.83 KiB) Viewed 571 times
I initialise the web page as below:

Code: Select all

		wb := ComObjCreate("InternetExplorer.Application")
		wb.Visible := True
		wb.Navigate(TrakcareAddress)
And the best way I found to catch the session is as below:

Code: Select all

				For IE in ComObjCreate("Shell.Application").Windows ; for each open window
				{
					If InStr(IE.FullName, "iexplore.exe") && InStr(IE.LocationName, TrakcareTitle) ; check if it's an ie window
					{
						TrakCareFound := True
						break
					}
				}
				
				if (TrakCareFound == True)
				{
					break
				}
And I set elements with code like below:

Code: Select all

					Results := IE.document.getelementsbyName(SearchValue)
					Results[0].value := Value
Has anyone else had issues with the COM object sporadically crashing? Am I not taking the best / most stable approach to interacting with IE? Any help will be appreciated.
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Internet Explorer COM failing sporadically

16 Dec 2020, 06:25

An element should always have tagname, element can have multiple attributes, like id, classname and name, but not every attribute has id, classname, name, or any special/custom attribe

According to following code You trying to get every tagname inside first frame,

Code: Select all

result := ie.document.all.tags("IFRAME")[0].contentWindow.document.getelementsbyName(SearchValue)
we can face such possibility, when there is no such frame called `right`, Or there is no tagname having attribute name as your searchvalue inside frame called `right` and that why we face error,
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
CotswoldsMaker
Posts: 12
Joined: 16 Dec 2020, 04:42

Re: Internet Explorer COM failing sporadically

09 Feb 2021, 19:21

Xeo786,

thanks for your reply. I have now completely changed how I initialise an IE object. This seems to have fixed the problem.

Code: Select all

IE := ComObjCreate("{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}") ; create a InternetExplorerMedium instance 
IE.Visible := True
IE.Navigate(anAddress)
IEBusy(IE)
And I grab open IE sessions like this

Code: Select all

IE := GrabOpenIESession(anAddress)
My IE library code (very basic) is as below. I intend to convert this into a neat class at some point.

Code: Select all

; Internet explorer library



;HTMLText := IE.document.documentElement.outerHTML
;HTMLText := IE.document.parentWindow.frames("frame name").document.documentElement.outerHTML
;HTML2Notepad(HTMLText)
HTML2Notepad(HTMLText)
{
	global IE
	
	clipboard := HTMLText
	Run, Notepad
	WinWait, Untitled - Notepad
	sleep 200
	Send, ^v
	return True
}




ClickOKIE()
{
	Path := A_WorkingDir . "\ClickOKIE.ahk"

	code =
	(
		Found := False
		MessageTitle := "Message from webpage"

		Loop 20
		{
			if WinExist(MessageTitle)
			{
				Found := True
				ControlClick, OK, Message from webpage
			}
			else if !WinExist(MessageTitle) AND Found
			{
				break
			}

			sleep 200
		}	

		FileDelete, %Path%		

		ExitApp
	)

	FPtr := FileOpen(Path, "w")
	FPtr.Write(code)
	FPtr.Close()

	run, % Path
	return True
}




; Use this to find a IE session if other tabs open
IEWinExist(WindowName)
{
	For IE in ComObjCreate("Shell.Application").Windows ; for each open window
	{
		If InStr(IE.FullName, "iexplore.exe") && InStr(IE.LocationName, WindowName)
		{
			return True
		}
	}

	return False
}




IEBusy(IE)
{
	sleepAmount := 50
	
	Loop, 5
	{
		try
		{
			while IE.busy || IE.ReadyState != 4
				sleep %sleepAmount%
		
			return True
		}
		catch
		{
			sleep 200
		}
	}

	throw Exception("IE fail", -1)
}




IESet(Frame := "", GetBy := "Name", SearchValue := "", Value := "", IE := "")
{
	Results := ""
	
	try
	{
		if (Frame = "")
		{
			if (GetBy = "Name")
			{
				;msgbox, "a"
				Results := IE.document.getelementsbyName(SearchValue)
				;msgbox, "b"
				Results[0].value := Value
				;msgbox, 1
				return True
			}
		}
		else
		{
			if (GetBy = "Name")
			{
				;msgbox, "c"
				Results := IE.document.parentWindow.frames(Frame).document.getElementsByName(SearchValue)
				;msgbox, "d"
				Results[0].value := Value
				;MsgBox, 2
				return True
			}
		}
	}
	catch
	{
		throw Exception("IE fail", -1)
	}
}




; !!! Have not fully tested
IEGetValue(Frame := "", GetBy := "Name", SearchValue := "", SearchValue2 := "", IE := "")
{
	Results := ""
	
	try
	{
		if (Frame = "")
		{
			if (GetBy = "Name")
			{
				Results := IE.document.getelementsbyName(SearchValue)
				return Results[0].value
			}
			else if (GetBy = "ID")
			{
				return IE.document.GetElementByID(SearchValue).value
			}
			else if (GetBy = "TagNameID")
			{
				Results := IE.document.GetElementsByTagName(SearchValue)
				
				Loop % Results.length
				{
					if (Results[A_index-1].ID == SearchValue2)
					{
						return Results[A_index-1].innerHTML
					}
				}
				
				return False
			}
		}
		else
		{
			if (GetBy = "Name")
			{
				Results := IE.document.parentWindow.frames(Frame).document.getElementsByName(SearchValue)
				return Results[0].value
			}
		}
	}
	catch
	{
		throw Exception("IE fail", -1)
	}
}




IEElementPresent(Frame := "", GetBy := "Name", SearchValue := "", IE := "")
{
	Results := ""

	try
	{
		if (Frame = "")
		{
			if (GetBy = "Name")
			{
				Results := IE.document.getelementsbyName(SearchValue)
				
				if (Results.Length == 0)
					return False
				else
					return True
			}
			else if (GetBy = "ID")
			{
				return IE.document.GetElementByID(SearchValue)
			}
		}
		else
		{
			if (GetBy = "Name")
			{
				try
					Results := IE.document.parentWindow.frames(Frame).document.getElementsByName(SearchValue)
				catch
					return False
					
				if (Results.Length == 0)
					return False
				else
					return True
			}
			else if (GetBy = "ID")
			{	
				return IE.document.parentWindow.frames(Frame).document.GetElementByID(SearchValue)
			}
		}
	}
	catch
	{
		throw Exception("IE fail", -1)
	}
}




IEClick(Frame := "", GetBy := "Name", SearchValue := "", SearchValue2 := "", IE := "")
{
	Results := ""
	
	try
	{
		if (Frame = "")
		{
			if (GetBy = "Name")
			{
				Results := IE.document.getelementsbyName(SearchValue)
				Results[0].click()
				IEBusy(IE)
				return True
			}
			else if (GetBy = "TagNameSRC")
			{
				Results := IE.document.GetElementsByTagName(SearchValue)

				Loop % Results.length
				{	
					if (InStr(Results[A_index-1].src, SearchValue2))
					{
						Results[A_index-1].click()
						IEBusy(IE)
						return True
					}
				}
				
				return False
			}
			else if (GetBy = "ID")
			{
				IE.document.GetElementByID(SearchValue).Click()
				IEBusy(IE)
				return True
			}
		}
		else
		{
			if (GetBy = "Name")
			{
				Results := IE.document.parentWindow.frames(Frame).document.getElementsByName(SearchValue)
				Results[0].click()
				IEBusy(IE)
				return True
			}
			else if(GetBy = "ID")
			{
				IE.document.parentWindow.frames(Frame).document.getElementById(SearchValue).Click()
				IEBusy(IE)
				return True
			}
			else if (GetBy = "TagNameSRC")
			{
				; !!! Have not actually tried this
				Results := IE.document.parentWindow.frames(Frame).document.GetElementsByTagName(SearchValue)

				Loop % Results.length
				{	
					if (InStr(Results[A_index-1].src, SearchValue2))
					{
						Results[A_index-1].click()
						IEBusy(IE)
						return True
					}
				}
				
				return False
			}

			else if (GetBy = "TagNameclassname")
			{
				Results := IE.document.parentWindow.frames(Frame).document.getElementsByTagname(SearchValue)

				Loop % Results.length
				{
					if (Results[A_index-1].getAttribute("classname") == SearchValue2)
					{
						Results[A_index-1].click
						IEBusy(IE)
						return True
					}		
				}
				
				return False
			}
			else if (GetBy = "TagNameTitle")
			{
				Results := IE.document.parentWindow.frames(Frame).document.GetElementsByTagName(SearchValue)

				Loop % Results.length
				{	
					if (InStr(Results[A_index-1].title, SearchValue2))
					{
						Results[A_index-1].click()
						IEBusy(IE)
						return True
					}
				}
				
				return False
			}
		}
	}
	catch
	{
		throw Exception("IE fail", -1)
	}
}




IEHTMLFind(Frame := "", GetBy := "Name", SearchValue := "", SearchValue2 := "", IE := "")
{
	Results := ""
	FramesList := ""
	HTMLText := ""
	
	try
	{
		if (Frame = "")
		{
			if (GetBy = "OuterHTML")
			{
				HTMLText := IE.document.documentElement.outerHTML
			
				if InStr(HTMLText, SearchValue)
					return True
				else
					return False
			}
			else (GetBy = "TagNameInnerHTML")
			{
				Results := IE.document.documentElement.getElementsByTagName(SearchValue)
				
				loop, % Results.length
				{
					if (InStr(Results[A_index-1].innerHTML, SearchValue2))
						return True
				}
				
				return False
			}
		}
		else
		{
			if (GetBy = "NameInnerHTML")
			{
				FramesList := IE.document.ParentWindow.Frames
						
				Loop % FramesList.length
				{
					if inStr(FramesList[A_index - 1].name, Frame) > 0
					{
						if inStr(FramesList[A_index - 1].document.getElementById(SearchValue).InnerHTML, SearchValue2) > 0
							return True
						else	
							return False
					}
				}
			}
			else (GetBy = "TagNameInnerHTML")
			{
				Results := IE.document.parentWindow.frames(Frame).document.getElementsByTagName(SearchValue)
				
				loop, % Results.length
				{
					if (InStr(Results[A_index-1].innerHTML, SearchValue2))
						return True
				}
				
				return False
			}
		}
	}
	catch
	{
		throw Exception("IE fail", -1)
	}
}




IEGet(Name="", HwndFilter="") 
{
	if (Name = "")	;// Get active window name if no parameter
		WinGetTitle, Name, % "ahk_id" HwndFilter:=WinExist("ahk_class IEFrame")
	else if (HwndFilter="A" or HwndFilter="Active")
		WinGet, HwndFilter, ID, ahk_class IEFrame
		
	Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft) Internet Explorer")
	
	for wb in ComObjCreate("Shell.Application").Windows
		if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
			if Not HwndFilter or (HwndFilter and wb.hwnd=HwndFilter)
				return wb
}



;[WBGet function for AHK v1.1]
;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) ;// based on ComObjQuery docs
{               
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   
   if (ErrorLevel != "FAIL") 
   {
      lResult:= ErrorLevel, VarSetCapacity(GUID,16,0)
	  
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 
	  {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}




GrabOpenIESession(Title)
{
	TabToTab(Title)
	;WinActivate, %Title%
	;WinGet, hWnd, ID, A
	WinGet, hWnd, ID, %Title%
	IE := WBGet("ahk_id " hWnd)
	IEBusy(IE)
	return IE
}




TabToTab(UrlOrTitle)
{
	Global found
	
	wb := IEGetFirst(UrlOrTitle)
	hwnd := wb.HWND
	
	if(found)
	{
		loop 
		{
			ControlGetText, wbUrl, Edit1, ahk_id %hwnd%
			WinGetTitle, wbTitle, ahk_id %hwnd%
			
			if(InStr(wbUrl, UrlOrTitle) or InStr(wbTitle, UrlOrTitle))
			{
				ControlSend, , {Ctrl up}, ahk_id %hwnd%
				Break
			}
			
			ControlSend, , {Ctrl down}{Tab}, ahk_id %hwnd%
			sleep, 100
		}
		
		WinActivate, ahk_id %hwnd%
	}
}




IEGetFirst(UrlOrTitle)
{
	Global found
	
	found := 0
	
	for wb in ComObjCreate("Shell.Application").Windows()
	{
		if(InStr(wb.FullName, "iexplore.exe"))
		{
			if(InStr(wb.LocationURL, UrlOrTitle) or InStr(wb.LocationName, UrlOrTitle))
			{
				found := 1
				Return wb
			}
		}
	}
}

CarlV
Posts: 5
Joined: 15 Jan 2021, 03:11

Re: Internet Explorer COM failing sporadically

15 Feb 2021, 11:54

Hi @CotswoldsMaker

could you put some examples on how to use your library?
image.png
image.png (19.98 KiB) Viewed 482 times
I have the following frame and I can't click it, do you have any suggestions?

thanks in advance
CotswoldsMaker
Posts: 12
Joined: 16 Dec 2020, 04:42

Re: Internet Explorer COM failing sporadically

18 Mar 2021, 15:57

Hi CarlV, Sorry for the late reply, I did not get any notification that you had mentioned me by name. I have now completely rewritten the IE code into a class. I can post it if you like.

Cheers
CarlV
Posts: 5
Joined: 15 Jan 2021, 03:11

Re: Internet Explorer COM failing sporadically

22 Mar 2021, 11:32

Hi @CotswoldsMaker
no problem, I imagined something like this.
I had also found the old library interesting, but I couldn't use it, post the new one if you like, I'll be happy to study it.
PS.
I would ask you to post some examples on how to use the various functions.

BR
CotswoldsMaker
Posts: 12
Joined: 16 Dec 2020, 04:42

Re: Internet Explorer COM failing sporadically

31 Mar 2021, 07:30

I am actually going to post alot of my work to GitHub soon, along with the IE library. Hopefully you are happy to wait to see it there.
CotswoldsMaker
Posts: 12
Joined: 16 Dec 2020, 04:42

Re: Internet Explorer COM failing sporadically

14 Apr 2021, 14:22

So I have a new IE library. Please see my work on Github. I have not created any examples on there, but hopefully it is straight forward to understand.

I simple way to start a new IE session would be:

Code: Select all

IE := new IE_control(URLAddress, WebsiteTitle)
ICE.click(, "TagNameSRC", "img", "images/USER.jpg")
https://github.com/Cotswoldsmaker/QuickSpiritum/blob/main/Library/Internet%20Explorer.ahk

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, ShatterCoder and 112 guests