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

Helpful script writing tricks and HowTo's
User avatar
Sjc1000
Posts: 39
Joined: 02 Oct 2013, 02:07

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

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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Download] (Script.ahk)GeSHi © Codebox Plus

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

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



Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


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.

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] [Download] (Script.ahk)GeSHi © Codebox Plus

[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] [Download] (Script.ahk)GeSHi © Codebox Plus

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: 1893
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
GitHub: ttnnkkrr
Location: Louisville KY
Contact:

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

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

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

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
n00b13
Posts: 3
Joined: 19 Nov 2013, 13:13
Google: 106079325882392276756

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

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.

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] [Download] GeSHi © Codebox Plus

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.

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: 5265
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

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] [Expand] [Download] GeSHi © Codebox Plus


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.

26 Oct 2016, 03:46

Find:
if (elements[A_Index - 1].innerText = "COMPOSE")
replace:
if (elements[A_Index - 1].innerText = text)
magusneo
Posts: 23
Joined: 30 Sep 2013, 06:34

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

05 Feb 2017, 05:58

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

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

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

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests