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

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
Sjc1000
Posts: 39
Joined: 02 Oct 2013, 02:07

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

Post by Sjc1000 » 25 Oct 2013, 23:55

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.
Please find me on the IRC if you have any questions, I'm never on the forum anymore.
n00b13
Posts: 3
Joined: 19 Nov 2013, 13:13
Google: 106079325882392276756

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

Post by n00b13 » 04 Dec 2013, 11:01

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.
User avatar
tank
Posts: 2373
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Louisville KY
Contact:

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

Post by tank » 04 Dec 2013, 11:54

i think its a matter of innertext vs value
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
User avatar
tank
Posts: 2373
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Louisville KY
Contact:

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

Post by tank » 04 Dec 2013, 11:58

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
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
n00b13
Posts: 3
Joined: 19 Nov 2013, 13:13
Google: 106079325882392276756

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

Post by n00b13 » 04 Dec 2013, 13:20

: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
User avatar
Sjc1000
Posts: 39
Joined: 02 Oct 2013, 02:07

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

Post by Sjc1000 » 25 Dec 2013, 04:23

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.
Please find me on the IRC if you have any questions, I'm never on the forum anymore.
torosyana
Posts: 1
Joined: 09 Jan 2016, 21:42

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

Post by torosyana » 09 Jan 2016, 21:52

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")
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Post by lexikos » 26 Oct 2016, 03:25

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.
Last edited by lexikos on 26 Oct 2016, 05:20, edited 1 time in total.
Reason: Fixed code.
ttt

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

Post by ttt » 26 Oct 2016, 03:46

Find:
if (elements[A_Index - 1].innerText = "COMPOSE")
replace:
if (elements[A_Index - 1].innerText = text)
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Post by lexikos » 26 Oct 2016, 05:20

:facepalm:
magusneo
Posts: 23
Joined: 30 Sep 2013, 06:34

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

Post by magusneo » 05 Feb 2017, 05:58

How can I save web images as local file using IE COM?
tmplinshi
Posts: 1264
Joined: 01 Oct 2013, 14:57

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

Post by tmplinshi » 05 Feb 2017, 06:42

magusneo wrote:How can I save web images as local file using IE COM?
WBImg - Get WebBrowser image without redownloading
magusneo
Posts: 23
Joined: 30 Sep 2013, 06:34

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

Post by magusneo » 05 Feb 2017, 08:08

It helps.Thank you.
serbring
Posts: 18
Joined: 04 Oct 2017, 11:46

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

Post by serbring » 22 Oct 2017, 07:59

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
User avatar
Blackholyman
Posts: 1271
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

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

Post by Blackholyman » 16 Nov 2017, 01:46

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
Also check out:
[hr][/hr]The Monthly AutoHotkey Webinars

My Autohotkey Blog
:dance: [hr][/hr]
xargon666

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

Post by xargon666 » 15 Jan 2018, 06:32

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>
KilledbyPing

Need some Help

Post by KilledbyPing » 24 Aug 2018, 15:32

#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
Post Reply

Return to “Tips and Tricks”