Jump to content

_IEAttach,How to make it more stable and reliable?


Recommended Posts

Quote

_IEAttach ( $sString, $sMode = "title" , $iInstance = 1)

In the case of the browser has been opened, this function, can be linked to the target web-page, simple and practical. However, instability sometimes occurs, for example, target web-page do exist, sometimes can be attached, sometimes attached failure. What are the main factors affecting reliability and how to ensure stability and reliability?

Edited by Letraindusoir
Link to comment
Share on other sites

59 minutes ago, Letraindusoir said:

What are the main factors affecting reliability and how to ensure stability and reliability?

I would say a lack of error checking in your script. 😏

P.S. Your post makes it sound like you think there's an issue with _IEAttach. I'm hoping that this is not your intent and it's simply due to a language barrier.

Link to comment
Share on other sites

On 1/9/2021 at 12:43 PM, Danp2 said:

I would say a lack of error checking in your script. 😏

P.S. Your post makes it sound like you think there's an issue with _IEAttach. I'm hoping that this is not your intent and it's simply due to a language barrier.

Thanks for attention to this small problem!

I am sorry that my english is not good! it Maybe have led to a misunderstanding.

I don't think there's an issue with _IEAttach,

 

I mean, I may not have mastered how to use the function.

Because in the process of use, the same browser, the same web-page, sometimes can be Attached, sometimes Attach failure. Whether a web-page is out of focus, or a window is deactivated, or a process problem causes the connection to be unstable?

error checking :

5 ($_IEStatus_InvalidValue)
7 ($_IEStatus_NoMatch)

But it doesn't seem to explain why

 


 

Edited by Letraindusoir
Link to comment
Share on other sites

On 1/10/2021 at 10:43 PM, Danp2 said:

If you can show us a short code snippet that consistently reproduces the issue, then we may be able to offer some suggestions on how to improve your code.

More thanks,Danp2

The code is similar to the help example. Because sometimes can, sometimes can not, is not sutre which website page appears this kind of situation, therefore feels the code is difficult to explain the problem. Maybe it's more reliable to use a handle, as Nine says

 

Link to comment
Share on other sites

  • 2 weeks later...
If $BrowserWindowHandle Then
   ConsoleWrite("$iWinState = " & WinGetState($BrowserWindowHandle) & @CRLF)
   ConsoleWrite("$BrowserWindowHandle = " & $BrowserWindowHandle & @CRLF)
   ;WinActivate($BrowserWindowHandle)
   $oIE = _IEAttach($BrowserWindowHandle, 'hwnd')
endif
$iWinState=23
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch

$iWinState=39
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch

$iWinState=39
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch

$iWinState=39
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch

$iWinState=7
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch


$iWinState=7
$BrowserWindowHandle = 0x00020462
--> IE.au3 T3.0-2 Warning from function _IEAttach, $_IESTATUS_NoMatch

Recently, there has been another uncertainty, even if the web-page is really there, and whether it is maximized or activated or minimize, the handle is undoubtedly available, will report errors.

Edited by Letraindusoir
Link to comment
Share on other sites

Func _IEAttach($sString, $sMode = "title", $iInstance = 1)
    $sMode = StringLower($sMode)

    $iInstance = Int($iInstance)
    If $iInstance < 1 Then
        __IEConsoleWriteError("Error", "_IEAttach", "$_IESTATUS_InvalidValue", "$iInstance < 1")
        Return SetError($_IESTATUS_InvalidValue, 3, 0)
    EndIf

    If $sMode = "embedded" Or $sMode = "dialogbox" Then
        Local $iWinTitleMatchMode = Opt("WinTitleMatchMode", $OPT_MATCHANY)
        If $sMode = "dialogbox" And $iInstance > 1 Then
            If IsHWnd($sString) Then
                $iInstance = 1
                __IEConsoleWriteError("Warning", "_IEAttach", "$_IESTATUS_GeneralError", "$iInstance > 1 invalid with HWnd and DialogBox.  Setting to 1.")
            Else
                Local $aWinlist = WinList($sString, "")
                If $iInstance <= $aWinlist[0][0] Then
                    $sString = $aWinlist[$iInstance][1]
                    $iInstance = 1
                Else
                    __IEConsoleWriteError("Warning", "_IEAttach", "$_IESTATUS_NoMatch")
                    Opt("WinTitleMatchMode", $iWinTitleMatchMode)
                    Return SetError($_IESTATUS_NoMatch, 1, 0)
                EndIf
            EndIf
        EndIf
        Local $hControl = ControlGetHandle($sString, "", "[CLASS:Internet Explorer_Server; INSTANCE:" & $iInstance & "]")
        Local $oResult = __IEControlGetObjFromHWND($hControl)
        Opt("WinTitleMatchMode", $iWinTitleMatchMode)
        If IsObj($oResult) Then
            Return SetError($_IESTATUS_Success, 0, $oResult)
        Else
            __IEConsoleWriteError("Warning", "_IEAttach", "$_IESTATUS_NoMatch")
            Return SetError($_IESTATUS_NoMatch, 1, 0)
        EndIf
    EndIf

    Local $oShell = ObjCreate("Shell.Application")
    Local $oShellWindows = $oShell.Windows(); collection of all ShellWindows (IE and File Explorer)
    Local $iTmp = 1
    Local $iNotifyStatus, $bIsBrowser, $sTmp
    Local $bStatus
    For $oWindow In $oShellWindows
        ;------------------------------------------------------------------------------------------
        ; Check to verify that the window object is a valid browser, if not, skip it
        ;
        ; Setup internal error handler to Trap COM errors, turn off error notification,
        ;     check object property validity, set a flag and reset error handler and notification
        ;
        $bIsBrowser = True
        ; Trap COM errors and turn off error notification
        $bStatus = __IEInternalErrorHandlerRegister()
        If Not $bStatus Then __IEConsoleWriteError("Warning", "_IEAttach", _
                "Cannot register internal error handler, cannot trap COM errors", _
                "Use _IEErrorHandlerRegister() to register a user error handler")
        ; Turn off error notification for internal processing
        $iNotifyStatus = _IEErrorNotify() ; save current error notify status
        _IEErrorNotify(False)

        ; Check conditions to verify that the object is a browser
        If $bIsBrowser Then
            $sTmp = $oWindow.type ; Is .type a valid property?
            If @error Then $bIsBrowser = False
        EndIf
        If $bIsBrowser Then
            $sTmp = $oWindow.document.title ; Does object have a .document and .title property?
            If @error Then $bIsBrowser = False
        EndIf

        ; restore error notify
        _IEErrorNotify($iNotifyStatus) ; restore notification status
        __IEInternalErrorHandlerDeRegister()
        ;------------------------------------------------------------------------------------------

        If $bIsBrowser Then
            Switch $sMode
                Case "title"
                    If StringInStr($oWindow.document.title, $sString) > 0 Then
                        If $iInstance = $iTmp Then
                            Return SetError($_IESTATUS_Success, 0, $oWindow)
                        Else
                            $iTmp += 1
                        EndIf
                    EndIf
                Case "instance"
                    If $iInstance = $iTmp Then
                        Return SetError($_IESTATUS_Success, 0, $oWindow)
                    Else
                        $iTmp += 1
                    EndIf
                Case "windowtitle"
                    Local $bFound = False
                    $sTmp = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\", "Window Title")
                    If Not @error Then
                        If StringInStr($oWindow.document.title & " - " & $sTmp, $sString) Then $bFound = True
                    Else
                        If StringInStr($oWindow.document.title & " - Microsoft Internet Explorer", $sString) Then $bFound = True
                        If StringInStr($oWindow.document.title & " - Windows Internet Explorer", $sString) Then $bFound = True
                    EndIf
                    If $bFound Then
                        If $iInstance = $iTmp Then
                            Return SetError($_IESTATUS_Success, 0, $oWindow)
                        Else
                            $iTmp += 1
                        EndIf
                    EndIf
                Case "url"
                    If StringInStr($oWindow.LocationURL, $sString) > 0 Then
                        If $iInstance = $iTmp Then
                            Return SetError($_IESTATUS_Success, 0, $oWindow)
                        Else
                            $iTmp += 1
                        EndIf
                    EndIf
                Case "text"
                    If StringInStr($oWindow.document.body.innerText, $sString) > 0 Then
                        If $iInstance = $iTmp Then
                            Return SetError($_IESTATUS_Success, 0, $oWindow)
                        Else
                            $iTmp += 1
                        EndIf
                    EndIf
                Case "html"
                    If StringInStr($oWindow.document.body.innerHTML, $sString) > 0 Then
                        If $iInstance = $iTmp Then
                            Return SetError($_IESTATUS_Success, 0, $oWindow)
                        Else
                            $iTmp += 1
                        EndIf
                    EndIf
                Case "hwnd"
                    If $iInstance > 1 Then
                        $iInstance = 1
                        __IEConsoleWriteError("Warning", "_IEAttach", "$_IESTATUS_GeneralError", "$iInstance > 1 invalid with HWnd.  Setting to 1.")
                    EndIf
                    If _IEPropertyGet($oWindow, "hwnd") = $sString Then
                        Return SetError($_IESTATUS_Success, 0, $oWindow)
                    EndIf
                Case Else
                    ; Invalid Mode
                    __IEConsoleWriteError("Error", "_IEAttach", "$_IESTATUS_InvalidValue", "Invalid Mode Specified")
                    Return SetError($_IESTATUS_InvalidValue, 2, 0)
            EndSwitch
        EndIf
    Next
    __IEConsoleWriteError("Warning", "_IEAttach", "$_IESTATUS_NoMatch")
    Return SetError($_IESTATUS_NoMatch, 1, 0)
EndFunc   ;==>_IEAttach
Local $oShell = ObjCreate("Shell.Application")
Local $oShellWindows = $oShell.Windows()
Case $sProperty = "hwnd"
     If Not __IEIsObjType($oObject, "browser") Then
        __IEConsoleWriteError("Error", "_IEPropertyGet", "$_IESTATUS_InvalidObjectType")
        Return SetError($_IESTATUS_InvalidObjectType, 1, 0)
    EndIf
    Return SetError($_IESTATUS_Success, 0, HWnd($oObject.HWnd()))

It feels like 'oShell.Windows' went wrong...

Link to comment
Share on other sites

53 minutes ago, Danp2 said:

This value doesn't change between runs, so my guess would be that you've grabbed the wrong window handle. Can't offer more that that because you haven't provided a way to reproduce your results. :rolleyes:

First, the web-page (TAB) has not been closed since it opened.
Second, it can be Attached normally at first.
Third, temporarily dis-Attached, and then open a new browser or new tab to browse some other web-pages.
Third, later, it was not Attached again, and the error was reported as above.

That is to say, the web-page has not been closed from beginning to end, and the web-page-window has always been there.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...