Page 1 of 1

[ ComObj IE ] Get an element from it's displayed text.

Posted: 25 Oct 2013, 23:55
by Sjc1000
This is a simple trick that will let you get an element in ComObj from its displayed text or innerText. It is useful for when the element has no ID or anything of the like.


Example that clicks the "COMPOSE" button in Gmail.

Code: Select all

While ( value <> "COMPOSE" )
	value 			:= Pwb.document.getElementsByTagName( "div")[ A_Index - 1].innerText, index := A_Index - 1

Pwb.document.getElementsByTagName( "div")[ index].Click()
Caution: If it throws an error you may need to change it to A_Index, not A_Index - 1


Other methods ( same code just moved around, Thanks to TLM ;) )

Code: Select all

While ( Pwb.document.getElementsByTagName( "div")[ A_Index - 1].innerText != "COMPOSE" )
         index 		:= A_Index - 1

Pwb.document.getElementsByTagName( "div")[ index].Click()

Code: Select all

While ( subObj := Pwb.document.getElementsByTagName( "div")[ A_Index - 1].innerText != "COMPOSE" )
         index 		:= A_Index

subObj.Click()

Explanation

It loops through the document, getting the innerText of each element that matches the 'tag'
getElementsByTagName( "div" )

Saving the value of A_Index ( so it can be used later ).
When the text / innerHTML matches the one you want it will break the while loop.
value <> "COMPOSE"

It then uses the saved A_Index value to operate on the correct element
Pwb.document.getElementsByTagName( "div")[ index].Click()


Working example

Code: Select all

Pwb				:= ComObjCreate( "InternetExplorer.Application" )													; Creates the ComObj
Pwb.Navigate("www.AutoHotkey.com")																					; Navigates to AutoHotkey.com
Pwb.Visible 		:= True																							; Shows the IE app

While ( Pwb.Busy || Pwb.ReadyState != 4 )																			; Waits for the webpage to finish loading
	Sleep 10																										;	|


value 				:= ""																							; Creates the variable ( not needed )

While ( value <> "Community Forum" )																				; Loops through the elements, until its innerText matches.
	value 			:= Pwb.document.getElementsByTagName( "a")[ A_Index - 1].innerText, index := A_Index -1			; Gets the value, and stores the Index

Pwb.document.getElementsByTagName( "a")[ index].Click()																; Clicks on the Community Forum link.
Done and done.. Hope this helps.

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 04 Dec 2013, 11:01
by n00b13
I am desperately trying to get this to work. its exactly what i need for a poorly written internal webpage we use at work.

what i am trying to find on the webpage is below (source) cant put all of it on here due to firewalls and corporate disclosure stuff...

Code: Select all

[Code=html5 file=Untitled.txt]                    <td width="25%" nowrap>
                        <form action="MopStatusAction.action" method="post">
                            <input name="imageField" type="submit" value="Work Complete">
                            <input name="mopStatusChange" type="hidden" value="mopComplete">
                            <input name="mopID" type="hidden" id="mopID" value='120413095821'>
                            <input type="hidden" name="mopCreatorEmpId" id="mopCreatorEmpId" value='e0148840'>
                        </form>
                    </td>
[/code]

it works out to a button on the top of the page that needs to be clicked.

what i have is

Code: Select all

value               := ""                                                                                           ; Creates the variable ( not needed )

While ( value <> "Work Complete" )                                                                                ; Loops through the elements, until its innerText matches.
    value           := Pwb.document.getElementsByTagName( "input")[ A_Index ].innerText, index := A_Index          ; Gets the value, and stores the Index
Pwb.document.getElementsByTagName( "input")[ index].Click()                                                             ; Clicks on the Community Forum link.

What am i missing because each time i use this code i end up with an error

Error: 0x80020006 - Unknown Name
Specifically : 35

hits on the Value := Pwb.document.getElementsByTagName( "input")[ A_Index ].innerText, index := A_Index


I tried with the '-1' and with out the '-1'

any ideas or help or emotional outburst are welcome. Thanks in advance for anything you can assist with.

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 04 Dec 2013, 11:54
by tank
i think its a matter of innertext vs value

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 04 Dec 2013, 11:58
by tank
BUTTON,INPUT,OPTION,SELECT,TEXTAREA all have value
please note that OPTION,SELECT,TEXTAREA will ALSO have an innertext. even so you will need to handle errors appropriately because people find amazing ways to break DOM rules

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 04 Dec 2013, 13:20
by n00b13
:lol:

Agreed, let me play with this a little more Thanks for the input Tank, I have read most all of your post you have been incredibly helpful to me in the past and in this instance as well.

Thanks for all you guys do and thanks for the input here as well

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 25 Dec 2013, 04:23
by Sjc1000
Sorry n00b13, i didn't even notice someone had posted on this. I need to check my own things more often :|

Ill have to thank tank who cleared things up with the innertext / value thing. My script goes off innerHTML, but with a small change it should use the value.

Code: Select all

While ( value <> "COMPOSE" )
    value           := Pwb.document.getElementsByTagName( "div")[ A_Index - 1].value, index := A_Index - 1

Pwb.document.getElementsByTagName( "div")[ index].Click()
( Not tested )

I just changed .innerHTML to .value
I guess you may have gotten confused since i use a variable called value.

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 09 Jan 2016, 21:52
by torosyana
Hi, I just want to say thank you, thank you. This helped me with my script for work.

Note: I am using wb.document instead of pwb. Used F12 for webpage to get exact value since noticed included spaces (ex. " Add " instead of "Add")

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 26 Oct 2016, 03:25
by lexikos
Since multiple users are apparently using the code from the first post, I thought I should point out some serious flaws:

The function would never return if it does not find the element, unless it throws up an error after passing the end of the array (it might not). I would recommend replacing While with Loop % Count, where Count is the number of elements in the array. When the element is found, break.

I'm fairly certain that getElementsByTagName() builds an array each time it is called. Therefore, the script builds a new array on each iteration. This is very inefficient, and also means that if some elements are added or removed (i.e. by scripts on the page), the loop could miss elements. It also "violates the DRY principle".

The index := A_Index - 1 part is also inefficient (but this is nothing compared to my previous point). index does not need to be assigned for every iteration; only when the element is found. If you're going to calculate it each iteration, it would make sense to do so before the [ A_Index - 1] part, and use [index] instead. Better yet, there's no need for index at all if you click() inside the loop.

Code: Select all

; Example:
ClickElementWithThisText("COMPOSE", Pwb.document, "div")

ClickElementWithThisText(text, document, tagName)
{
    elements := document.getElementsByTagName(tagName)
    Loop % elements.length
        if (elements[A_Index - 1].innerText = text)
        {
            elements[A_Index - 1].click()
            break
        }
}
Untested.

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 26 Oct 2016, 03:46
by ttt
Find:
if (elements[A_Index - 1].innerText = "COMPOSE")
replace:
if (elements[A_Index - 1].innerText = text)

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 26 Oct 2016, 05:20
by lexikos
:facepalm:

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 05 Feb 2017, 05:58
by magusneo
How can I save web images as local file using IE COM?

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 05 Feb 2017, 06:42
by tmplinshi
magusneo wrote:How can I save web images as local file using IE COM?
WBImg - Get WebBrowser image without redownloading

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 05 Feb 2017, 08:08
by magusneo
It helps.Thank you.

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 22 Oct 2017, 07:59
by serbring
Hi ,

I run the following script:

Code: Select all

	Pwb				:= ComObjCreate( "InternetExplorer.Application" )								
Pwb.Navigate("www.AutoHotkey.com")																					
Pwb.Visible 		:= True																							

ClickElementWithThisText("FORUMS", Pwb.document, "div")

ClickElementWithThisText(text, document, tagName)
{
    elements := document.getElementsByTagName(tagName)
    Loop % elements.length
        if (elements[A_Index - 1].innerText = text)
        {
            elements[A_Index - 1].click()
            break
        }
}
However, I get an error at the following line:

Code: Select all

ClickElementWithThisText("FORUMS", Pwb.document, "div")
What's wrong?

I'm using the 1.1.26.01 version.

Thanks

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 16 Nov 2017, 01:46
by Blackholyman
serbring wrote:However, I get an error at the following line:
well you need to wait for the page to load something like this...

Code: Select all

wb	:= ComObjCreate( "InternetExplorer.Application" )								
wb.Navigate("www.AutoHotkey.com")
while wb.busy
 sleep 100
wb.Visible 		:= True																							
ClickElementWithThisText("FORUMS", wb.document, "div")
return

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 15 Jan 2018, 06:32
by xargon666
I'm struggling with this one still :/ I have a pretty basic grasp of AHK.

Here's what I'm trying to press:
<BUTTON onclick=setKeys(event);__x4a9fonclick(this); title="Next Page" class="button buttonLink" name=PierPropertiesContainer_componentnext_0>Next</BUTTON>

Need some Help

Posted: 24 Aug 2018, 15:32
by KilledbyPing

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.

#SingleInstance, force


;--------------------
;GUI Look------
;--------------------




Gui, Font, s15
Gui, Add, Text, x400,  Giftcard "Generator"
Gui, color, red
Gui, Show, w1000 h600, Money Generator by KilledbyPing
msgbox, Created by KilledbyPing



;------------
;GUI Buttons--
;------------


Gui, Add, Button, x40 y70 w200 h100 gSwagG  ,SwagBucks Glitch
return

;------------
;GUI lable--
;-------------

	
;----------------------------------------------------------------------------------------------------------



SwagG:
	{
      ; a  := add (ProcessSwag)
      msgbox,  Press "Ok" to Start farming Swagbucks. After pressing "Ok" the process will start 2 seconds later.                                                                                                                                                                                                                                !!                   F11 to Reload , F12 to Close                    !!
	  	sleep 1200
	
	
	Pwb				:= ComObjCreate( "InternetExplorer.Application" )												
Pwb.Navigate("http://www.swagbucks.com/surveys?p=1&m=4&ls=7&a=5")																					
Pwb.Visible 		:= True																						

While ( Pwb.Busy || Pwb.ReadyState != 4 )																			
	Sleep 100

While ( value <> "Wählen Sie Ihre Antwort " )																				; Loops through the elements, until its innerText matches.
	value 		:= Pwb.document.getElementsByTagName( "a")[ A_Index - 1].innerText, index := A_Index 1			; Gets the value, and stores the Index

Pwb.document.getElementsByTagName( "a")[ index].Click()			


return


  }

I need some Help boiss so i need that tho think press on the Button where i can answer the task http://www.swagbucks.com/surveys?p=1&m=4&ls=7&a=5


Idk how to go on In getting an error on : value := Pwb.document.getElementsByTagName( "a")[ A_Index - 1].innerText, index := A_Index 1

Re: [ ComObj IE ] Get an element from it's displayed text.

Posted: 30 Nov 2021, 09:26
by Nick A
I've adapted this to find partial strings as well:

Code: Select all

Pwb := ComObjCreate( "InternetExplorer.Application" )	; Creates the ComObj
Pwb.Navigate("www.AutoHotkey.com")				; Navigates to AutoHotkey.com
Pwb.Visible := True								; Shows the IE app

While ( Pwb.Busy || Pwb.ReadyState != 4 )		; Waits for the webpage to finish loading
	Sleep 10																										;	|


Value  := ""  			; Creates the variable (not needed)
Needle := "ear"			; Creates the search string (needed)
In_String_Check :=0		; Creates the string check (needed)

While ( In_String_Check=0 )
{
	Value := Pwb.document.getElementsByTagName( "a")[ A_Index - 1].innerText   ; Gets the value
	index := A_Index -1	;stores the Index
	;MsgBox % Value
	In_String_Check := Instr(Value, Needle)  ;will equal 0 unless the search string is found
	;MsgBox % In_String_Check
}
Pwb.document.getElementsByTagName( "a")[ index].Click() 	;Clicks the search link