COM Element Value Retrieval Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lVlrBoJang1es
Posts: 7
Joined: 20 Aug 2018, 08:36

COM Element Value Retrieval

20 Aug 2018, 13:07

I'm working on an AHK project that will "scrape" values from a machine's Internet Explorer based interface.

Every time i've done web page scraping in the past, i feel like i "get lucky" when my program is finally able to extract the data that i'm targeting. So far i was only able to get "partially lucky" with this project. My program can currently grab one set of values, but i'm having a hard time grabbing a (nearly identical) 2nd set of values from the web interface.

Here is a snippet of the relevant parts of my program:

Code: Select all

;(* Initialize IExplorer COM Object *)
wb               := IEGet("Operator View - IRIS081101")                                             ;(* Create pointer to IE window     *)
wb.Visible       := True                                                                            ;(* Unhide IE window                *)


;(* ++===============================================================================================================================++ *)
;(* || Grab Data from Operator Web Interface                                                                                         || *)
;(* ++===============================================================================================================================++ *)
;(*                                                                                                                                     *)
;(* Machine Counts -------------------------------------------------------------------------------------------------------------------- *)
;(* Batches *)                                                                                      ;(*                                 *)
iBatchesExpected    := wb.document.all.CR_Exp_Batches.Value                                         ;(* Pull Expected Batches #         *)
iCurrentBatchNum    := wb.document.all.CR_Current_Batches.Value


;(* ++===============================================================================================================================++ *)
;(* || Functions                                                                                                                     || *)
;(* ++===============================================================================================================================++ *)
;(*                                                                                                                                     *)
;(*  Retrieve Internet Explorer Pointer ----------------------------------------------------------------------------------------------- *)
IEGet(Name="")                                                                                      ;(*                                 *)
{                                                                                                   ;(*                                 *)
    IfEqual, Name,, WinGetTitle, Name, 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 ) && InStr( wb.FullName, "iexplore.exe" )                       ;(*                                 *)
            Return wb                                                                               ;(* Return pointer to IE window     *)
}                                                                                                   ;(*                                 *)
;(* ----------------------------------------------------------------------------------------------------------------------------------- *)
The program pulls the iBatchesExpected value appropriately.
The iCurrentBatchNum value however comes through as "(Object)" when viewed in the Variable List window of SciTE editor <-- I should be retrieving a value in this case.

Oddly, these two elements share nearly identical code (from what i can tell) in the "DOM Explorer" window after performing an Inspect Element on each:

Expected Batches element Code:

Code: Select all

<INPUT class=placeholder id=CR_Exp_Batches style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 16px; CURSOR: auto; HEIGHT: 21px; BORDER-RIGHT-WIDTH: 0px; WIDTH: 110px; BORDER-BOTTOM-WIDTH: 0px; FONT-WEIGHT: bold; COLOR: black; TEXT-ALIGN: right; PADDING-RIGHT: 10px; BORDER-TOP-WIDTH: 0px; BACKGROUND-COLOR: transparent" contentEditable=false value="Project ended." datype="Value" operatorviewname="testing">
From the debugger:

Code: Select all

<TD class=datb0_GridStyle style="FONT-SIZE: 16px; WIDTH: 121px; PADDING-BOTTOM: 0px; TEXT-ALIGN: right; PADDING-TOP: 0px" cellStyle="0">
<DIV class=custom contentEditable=false dahtml="custom"><INPUT id=CR_Exp_Batches class=placeholder contentEditable=false style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 16px; HEIGHT: 21px; BORDER-RIGHT-WIDTH: 0px; WIDTH: 110px; BORDER-BOTTOM-WIDTH: 0px; FONT-WEIGHT: bold; COLOR: black; TEXT-ALIGN: right; PADDING-RIGHT: 10px; BORDER-TOP-WIDTH: 0px; BACKGROUND-COLOR: transparent" value='{Flowcharts("Controls").Variables.CS_Exp_Batches:F0}' datype="Value"></DIV></TD>
Current Batches element Code:

Code: Select all

<INPUT class=placeholder id=CR_Current_Batches style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 16px; CURSOR: auto; HEIGHT: 21px; BORDER-RIGHT-WIDTH: 0px; WIDTH: 110px; BORDER-BOTTOM-WIDTH: 0px; FONT-WEIGHT: bold; COLOR: blue; TEXT-ALIGN: right; PADDING-RIGHT: 10px; BORDER-TOP-WIDTH: 0px; BACKGROUND-COLOR: transparent" contentEditable=false value="Project ended." datype="Value" operatorviewname="testing">
From the debugger:

Code: Select all

<TD class=datb0_GridStyle style="FONT-SIZE: 16px; WIDTH: 121px; PADDING-BOTTOM: 0px; TEXT-ALIGN: right; PADDING-TOP: 0px" cellStyle="1">
<DIV class=custom contentEditable=false dahtml="custom"><INPUT id=CR_Current_Batches class=placeholder contentEditable=false style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 16px; HEIGHT: 21px; BORDER-RIGHT-WIDTH: 0px; WIDTH: 110px; BORDER-BOTTOM-WIDTH: 0px; FONT-WEIGHT: bold; COLOR: blue; TEXT-ALIGN: right; PADDING-RIGHT: 10px; BORDER-TOP-WIDTH: 0px; BACKGROUND-COLOR: transparent" value='{(PR_Data.Fields("CR_Current_Batch").Value + 1):F0}' datype="Value"></DIV></TD>
From what i can tell, the parental hierarchy above these two elements is the exact same in the web page code. Both of these elements are also dynamic; the "expected batches" value is populated by operator input on a previous page, the "current batch number" value is pulled from a PLC.

When disconnected from the machine, these two fields are populated with a "Project ended." text string. This is the output i'm seeing stored in the "iExpectedBatches" variable, but the "iCurrentBatchNum" contains "(Object)" - I would expect both references would return the same value since both fields contain "Project ended." on the interface when offline.

Any help is appreciated!

(I would post the page source but it was way over the character limit for the post)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: COM Element Value Retrieval

21 Aug 2018, 09:31

give a pastebin link to the source
document.all is a deprecated, try wb.document.getElementById("CR_Exp_Batches").value instead
lVlrBoJang1es
Posts: 7
Joined: 20 Aug 2018, 08:36

Re: COM Element Value Retrieval

21 Aug 2018, 11:16

swagfag wrote:give a pastebin link to the source
document.all is a deprecated, try wb.document.getElementById("CR_Exp_Batches").value instead
Thanks for the reply.

Had to use "Hastebin" due to firewall. Hopefully this works:
https://hastebin.com/unakutozic.cs

I've tried several different referencing methods thus far. Off the top of my head:

Test1 := wb.document.getElementById("CR_Current_Batches").innerHTML (returns nothing)
Test2 := wb.document.getElementById("CR_Current_Batches").value (returns "0") <-- i turned around and tested this on the live machine connected interface and the value on the interface was NOT 0.
Test3 := wb.document.all("CR_Current_Batches") (returns "(Object)")
Test 4 := wb.document.all.CR_Current_Batches (returns "(Object)")

I started with the iWebBrowser2 Learner (https://autohotkey.com/board/topic/8425 ... bbrowser2/). This was the output:

Element Under Mouse:
Index:573 Name: ID: CR_Current_Batches
OuterHTML:

Code: Select all

<INPUT class=placeholder id=CR_Current_Batches style="BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 16px; CURSOR: auto; HEIGHT: 21px; BORDER-RIGHT-WIDTH: 0px; WIDTH: 110px; BORDER-BOTTOM-WIDTH: 0px; FONT-WEIGHT: bold; COLOR: blue; TEXT-ALIGN: right; PADDING-RIGHT: 10px; BORDER-TOP-WIDTH: 0px; BACKGROUND-COLOR: transparent" contentEditable=false value="Project ended." datype="Value" operatorviewname="testing">
Let me know if I can supply any further information that might help! Really scratching my head on this one!!
lVlrBoJang1es
Posts: 7
Joined: 20 Aug 2018, 08:36

Re: COM Element Value Retrieval

21 Aug 2018, 14:03

I got it working recently, not sure exactly why.

The syntax i used:
wb.document.all("CR_Current_Batches")[1].value

Once again an instance where i feel like i "got lucky". I literally fat fingered a "1" in the index instead of a 0 right before running the code one last time before lunch break. I was expecting an index out of range error of sorts, but surprisingly it returned the correct value from the interface.

On to the fun part of the project!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: COM Element Value Retrieval  Topic is solved

22 Aug 2018, 03:28

2 elements with the same id, just lol... u can thank whoever designed the page for your woes
getElementById()/querySelector() return only the first element that matches the id. it appears u were interested in the second, though

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, sharonhuston and 224 guests