uncommon

_IEgetobjbyclassname()

5 posts in this topic

#1 ·  Posted (edited)

I think this has been around in IE for a while. Seems useful. Would it be possible to put something like this into the next IE UDF update? I know dale is the one who manages it. Maybe he can chime in? BTW I pretty much changed the settings around from _IEgetobjbyname()

;#FUNCTION#=============================================================================================================
; Name...........: _IEClassNameGetCollection()
; Description ...: Returns an Object or a Colection of objects
; Syntax.........: (ByRef $o_object, $s_ClassName, $i_index = -1)
; Parameters ....: $o_object - The document object from IE
;                  $s_ClassName - The Name of the class to reference
;                  $i_index -     [optional] -1 (default) returns a colection, else use a 0 based index to indicate the object in sequence
; Return values .: Success - Object or a Colection of objects
;                  Failure - returns 0 sets the @error flag to non-zero.
;                   3 ($_IEStatus_InvalidDataType) - Invalid Data Type
;                   4 ($_IEStatus_InvalidObjectType) - Invalid Object Type
;                   5 ($_IEStatus_InvalidValue) - Invalid Value
;                   7 ($_IEStatus_NoMatch) - No Match
; Author ........: XThrax aka uncommon
; Remarks .......: This fuctions was pretty much stolen from Dale Hohm the IE UDF creator so much credit to him.
;                  
; Related .......: _IETagNameAllGetCollection()
; Link ..........;https://www.autoitscript.com/forum/topic/177201-_iegetobjbyclassname
;                 https://msdn.microsoft.com/en-us/library/ff975198(v=vs.85).aspx
; Example .......; No
; =====================================================================================================================
Func _IEClassNameGetCollection (ByRef $o_object, $s_ClassName, $i_index = -1)
    If Not IsObj($o_object) Then
        __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidDataType")
        Return SetError($_IEStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __IEIsObjType($o_object, "browserdom") Then
        __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidObjectType")
        Return SetError($_IEStatus_InvalidObjectType, 1, 0)
    EndIf

    Local $oTemp
    If __IEIsObjType($o_object, "documentcontainer") Then
        $oTemp = _IEDocGetObj($o_object)
    Else
        $oTemp = $o_object
    EndIf

    $i_index = Number($i_index)
    Select
        Case $i_index = -1
            Return SetError($_IEStatus_Success, $oTemp.getElementsByClassName($s_ClassName).length, _
                    $oTemp.getElementsByClassName($s_ClassName))
        Case $i_index > -1 And $i_index < $oTemp.getElementsByClassName($s_ClassName).length
            Return SetError($_IEStatus_Success, $oTemp.getElementsByClassName($s_ClassName).length, _
                    $oTemp.getElementsByClassName($s_ClassName).item($i_index))
        Case $i_index < -1
            __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidValue", "$i_index < -1")
            Return SetError($_IEStatus_InvalidValue, 3, 0)
        Case Else
            __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_NoMatch")
            Return SetError($_IEStatus_NoMatch, 0, 0) ; Could be caused by parameter 2, 3 or both
    EndSelect
EndFunc   ;==>_IEClassNameGetCollection

https://msdn.microsoft.com/en-us/library/ff975198(v=vs.85).aspx

Edited by uncommon
Function revised from _IETagNameAllGetCollection()
1 person likes this

No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Nice

I would set the default value of $iIndex to -1 instead of 0

edit:
also, maybe a function name like _IEClassGetCollection (or something similar)  wouldn't be more consistent with the other _IE* functions that returns a collection or a single obj by index?

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Nice

I would set the default value of $iIndex to -1 instead of 0

edit:
also, maybe a function name like _IEClassGetCollection (or something similar)  wouldn't be more consistent with the other _IE* functions that returns a collection or a single obj by index?

II didn't think about that. Revised. 

I used an older function that would take forever with for high index numbers because it would loop count EVERY class and remember the ones that had the correct name. Needless to say, this was 100 times faster.


No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites

Recently I learn that this should like this:

;#FUNCTION#=============================================================================================================
; Name...........: _IEClassNameGetCollection()
; Description ...: Returns an Object or a Colection of objects
; Syntax.........: (ByRef $oIE, $sClassName, $iItemIndex = Default)
; Parameters ....: $oIE - The document object from IE
;                  $sClassName - The Name of the class to reference
;                  $iItemIndex -     [optional] Default returns a colection, else use a 0 based index to indicate the object in sequence
; Return values .: Success - Object or a Colection of objects
;                  Failure - returns 0 sets the @error flag to non-zero.
;                   3 ($_IEStatus_InvalidDataType) - Invalid Data Type
;                   4 ($_IEStatus_InvalidObjectType) - Invalid Object Type
;                   5 ($_IEStatus_InvalidValue) - Invalid Value
;                   7 ($_IEStatus_NoMatch) - No Match
; Author ........: XThrax aka uncommon
; Modified ......: mLipok
; Remarks .......: This fuctions was pretty much stolen from Dale Hohm the IE UDF creator so much credit to him.
;
; Related .......: _IETagNameAllGetCollection()
; Link ..........;https://www.autoitscript.com/forum/topic/177201-_iegetobjbyclassname
;                 https://msdn.microsoft.com/en-us/library/ff975198(v=vs.85).aspx
; Example .......; No
; =====================================================================================================================
Func _IEClassNameGetCollection(ByRef $oIE, $sClassName, $iItemIndex = Default)
    If Not IsObj($oIE) Then
        ; __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidDataType")
        Return SetError($_IEStatus_InvalidDataType, 1, 0)
    ElseIf Not __IEIsObjType($oIE, "browserdom") Then
        ; __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidObjectType")
        Return SetError($_IEStatus_InvalidObjectType, 1, 0)
    ElseIf Not IsNumber($iItemIndex) Then
        Return SetError($_IEStatus_InvalidValue, 3, 0)
    ElseIf $iItemIndex = Default Or $iItemIndex >= 0 Then
        Local $oTemp = Null
        If __IEIsObjType($oIE, "documentcontainer") Then
            $oTemp = _IEDocGetObj($oIE)
            If @error Then Return SetError(@error,@extended,0)
        Else
            $oTemp = $oIE
        EndIf

        Local $oClassColl = $oTemp.getElementsByClassName($sClassName)
        If @error Then
            ; __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IESTATUS_GeneralError", "")
            Return SetError($_IESTATUS_GeneralError, 3, 0)
        ElseIf (Not IsObj($oClassColl)) Or $oClassColl = Null Or $oClassColl.length = 0 Then
            ; __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_NoMatch")
            Return SetError($_IEStatus_NoMatch, 0, 0) ; Could be caused by parameter 2, 3 or both
        Else
            If $iItemIndex = Default Then
                Return SetError($_IEStatus_Success, $oClassColl.length, $oClassColl)
            ElseIf $iItemIndex > $oClassColl.length Then
                Return SetError($_IEStatus_InvalidValue, $oClassColl.length, 0)
            Else
                $oItem = $oClassColl.Item($iItemIndex)
                If @error Then
                    Return SetError($_IESTATUS_GeneralError, 3, 0)
                ElseIf (Not IsObj($oItem)) Or $oItem = Null Then
                    Return SetError($_IEStatus_NoMatch, 0, 0) ; Could be caused by parameter 2, 3 or both
                Else
                    Return SetError($_IEStatus_Success, 0, $oItem)
                EndIf
            EndIf
        EndIf

    Else
        ; __IEConsoleWriteError("Error", "_IEClassNameGetCollection", "$_IEStatus_InvalidValue", "$iItemIndex < -1")
        Return SetError($_IEStatus_InvalidValue, 3, 0)
    EndIf

EndFunc   ;==>_IEClassNameGetCollection

 

REMARK: Not tested.


Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites
On 10.09.2015 at 9:25 AM, uncommon said:

Would it be possible to put something like this into the next IE UDF update?

I think: Yes.


Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
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

  • Similar Content

    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning
      I would like to know if I can use the Excel UDF to manipulating a .csv file without having Office installed on the PC I'm going to work...
      I read somewhere that it could be done, but I'm here to ask and be sure of what I remember... 
      I'd like to post another question...
      How can I retrieve the handle of a windows from a PID of an .exe?
      I have my script that does a ShellExecute ( which returns the PID of the .exe ), and then, switching a parameter read from a .ini file, adapt the Window on the screen ( Maximize, Minimize, On Top )...
      I tried, but without having success with this:
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile_x64=prova.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <WinAPIEx.au3> #include <Array.au3> Local $sFileConfigurazione = @ScriptDir & "\configurazione_exe.ini" If(FileExists($sFileConfigurazione)) Then Local $aSezioniIni = IniReadSection($sFileConfigurazione, "CONFIGURAZIONE_EXE") If @error Then MsgBox($MB_ICONERROR, "Errore!", "Errore durante l'apertura del file: " & @CRLF & $sFileConfigurazione & @CRLF & "Errore: " & @error) Else ; Lancio dell'applicativo indicato nel file di configurazione Local $iPID = ShellExecute($aSezioniIni[1][1]) Local $hWnd If($iPID <> 0) Then Local $aWinList = WinList() For $i = 1 To $aWinList[0][0] If(WinGetProcess($aWinList[$i][1] = $iPID)) Then $hWnd = $aWinList[$i][1] EndIf Next Switch($aSezioniIni[2][1]) Case $aSezioniIni[2][1] = "MIN" WinSetState($hWnd, "", @SW_MINIMIZE) Case $aSezioniIni[2][1] = "MAX" WinSetState($hWnd, "", @SW_MAXIMIZE) Case $aSezioniIni[2][1] = "TOP" WinSetOnTop($hWnd, "", $WINDOWS_ONTOP) EndSwitch EndIf EndIf EndIf It just set on top the .exe I'm launching...
      Thanks
    • elbhiri
      By elbhiri
      i have two files a.au3 and b.au3 the both contains the same code which is :
      #include <IE.au3> #include <MsgBoxConstants.au3> Global $myUrl = "https://www.autoitscript.com" RegWrite('HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main','SessionMerging','REG_DWORD','00000000') ShellExecute ("iexplore.exe", " -nosessionmerging about:blank") WinWait ("Blank Page") Global $oIE = _IEAttach ("about:blank", "url") _IELoadWait ($oIE) _IENavigate ($oIE, $myUrl) when i launch the both files in the same time two blank windows are opens.
      what im waiting for is that every window navigate to the link but what happen is the first and the second file navigate the url to the same window and the seconde window stay about:blank
      i think the problem happen because i try to open the same url in both windows
      so i need i way to navigate to the same url from two different windows
      Thanks in advance
    • HudsonKane
      By HudsonKane
      I have a page with several hundred <div>s, and each <div> has several hundred child <div>s, each with their own children
      when I use _IETagNameGetCollection($ie, "div", 0) I get them all, but I need to find a way to get say the 3rd child of the 5th child of the second top level <div>
      I have not yet been able to find anything along this line
      Is there any way to determine if an element is a child, parent, sibling?
      Thank you
    • TheSaint
      By TheSaint
      I had a need, and so I developed.
      Before I did that though, I did a quick search of the forum, and found a few things, but none seemed to suit what I really wanted ... though to be honest, I did not investigate them deeply or search extensively. In any case, I am not very good at modifying the code of others, nor do I enjoy doing so, so in the end I decided it was best to start from scratch ... re-inventing the wheel perhaps, but a wheel I feel a certain degree of comfort with. I also thought about Maps, but don't know enough.
      _IniString Functions (was Ini_InMem)
      Ini in memory
      Settings INI - Read/Write once to the disk
      INI File Processing Functions
      ... sure to be others.
      As with all my stuff, feel free to use (no guarantees) or modify ... just give me credit where due.
      I always go for the quick & simple approach, as those who know me here, are well aware, so without a doubt, my code could be improved - sped up, RegExp used, etc. So feel free to do that, and convert into a proper UDF, if you want, following best practices etc, as I am sure others will appreciate it.
      Recently, I have come upon a need to extract data from an approximately 50 Mb downloaded Index file, just extracting the few elements I need, and storing them in an INI file. It is quite a time consuming process, especially on my underpowered Netbook, which is where I am using the program I built. I have been considering ways to speed up things. Two good sounding methods, out of several possibles, have come to mind - 1) RAM Disk and 2) Virtual INI processing. That last being a better option, when sharing code (program) with others.
      Another method, which I am currently using, that did give me some measurable benefit, was to download the almost 50 Mb Index file in Stages (i.e. 30 x 1.4 Mb roughly), extracting data from each in turn. I have also considered extracting to 10 (or 11) separate INI files, rather than the single large INI file (4~5 Mb), as things noticeably slow up as the INI file increases in size ... obviously due to writing time.
      I can't really speed up the extraction process, but that last method may speed up the writing stage.  However, it would require significant program changes, and perhaps not gain me much benefit. To complicate things, there are too many Section names (Index ID's) for the IniReadSectionNames command, so I have had to split them off into their own (read/write) line-by-line index file, and create code to deal with duplicates. When read, Index ID's and Titles populate a Listbox control in a user GUI.
      So at this point in time, it seems best to use the Virtual INI approach to gain a significant reduction in time taken. For my own specific use, with my slow Netbook, I may also use a RAM Disk.
      Due to the aforementioned INI issues, and lack of available specific data, probably because it is always  program concentric, I have created an ASCII Checker program, which I guess you can say, is AutoIt  concentric. Because others may also find it handy, I am providing it here. NOTE - As yet, I am only aware that Escape characters exist, but have not investigated or catered for them in any way.
      ASCII Checker.zip

      The Virtual_INI_UDF is still a work in progress, though I have done most of the functions now, with the working but incomplete _Ini_Test function, still requiring changes etc from the information gleaned by the ASCII Checker program.
      Here's something to play with meanwhile.
      Updated files (24th February 2017)
      Virtual_INI_UDF.au3  NEW
      Example.au3 (23rd February 2017)
      All pretty basic, but managed to load my 4 Mb INI file (seemingly ok, but took a few seconds) ... though I have only worked with much smaller testing ones so far.
      P.S. While I have done a fair amount of testing, it has been pretty basic, and mostly limited to the examples provided. Testing has definitely not been extensive.
       
    • mjolnirmarkiv
      By mjolnirmarkiv
      Hi!
      Does anyone have any idea why Test() returns 0, while Test2() returns 1?
      #include <IE.au3> MsgBox(0, "", Test() & " " & Test2()) Func Test() Local $oHTML = ObjCreate("HTMLFILE") $oHTML.body.innerHTML = FileRead("Test.html") Return IsObj($oHTML.getElementsByClassName("example")) EndFunc Func Test2() Local $oIE = _IEAttach("", "instance", 1) Return IsObj($oIE.document.getElementsByClassName("example")) EndFunc Sample Test.html I open in IE before testing:
      <!DOCTYPE html> <html> <body> <p class="example"></p> <div class="example"></div> </body> </html> I'm receiving html text in a response to http request, I need to parse it fast and easy, getElementsByClassName would have been ideal. I'm using 3.3.12.0 version.