AHK: IE COM_getElementsByTagName Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

AHK: IE COM_getElementsByTagName

15 Jan 2019, 08:45

Hello. Tell me please how to know item if i know the value, example :

Code: Select all

ie.document.getElementsByTagName("TD")[507].innerText := "1005454357"
. I know "1005454357", how do i know "507" ?

Code: Select all

<a target="_blank" data-bind="text:LastName() + ' ' + FirstName()+ ' ' + SecondName(), attr:{'data-profile-id':$data.TicketID, 'href': '/********/SearchVisitors/VisitorInfo' + '?profileId=' + $data.ProfileID()}" data-profile-id="1005454357" href="/**********/SearchVisitors/VisitorInfo?profileId=2413703">Family Name</a>
rommmcek
Posts: 674
Joined: 15 Aug 2014, 15:18

Re: AHK: IE COM_getElementsByTagName

15 Jan 2019, 10:04

Maybe:

Code: Select all

loop, % ie.document.getElementsByTagName("TD").length
	 if ie.document.getElementsByTagName("TD")[A_Index-1].value == "1005454357"
		MsgBox % A_Index-1
swagfag
Posts: 2907
Joined: 11 Jan 2017, 17:59

Re: AHK: IE COM_getElementsByTagName

15 Jan 2019, 10:45

legacy-if + case-sensitive comparison, what a disaster. ure comparing var to an equal sign =
rommmcek
Posts: 674
Joined: 15 Aug 2014, 15:18

Re: AHK: IE COM_getElementsByTagName

15 Jan 2019, 11:20

@swagfag: I agree I'm no expert! But:

Code: Select all

loop, % wb.document.getElementsByTagName("INPUT").length
	 if wb.document.getElementsByTagName("INPUT")[A_Index-1].value == "Login"
		wb.document.getElementsByTagName("INPUT")[A_Index-1].click()
works! (I just logged in with it)
The value could be a string too! So it's more universal, I guess! Any other alternatives are welcome!
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: AHK: IE COM_getElementsByTagName

15 Jan 2019, 11:27

I wonder how this can works... And to add another layer or make the strokes bolder... :mrgreen: your calling getElementsByTagName... needlessly: it has nothing 'loopy' here :bravo: :mrgreen: I can only guess you write this code with inadvertence, rommmcek :!: :?:

Code: Select all

loop % (HTMLAnchorElements:=ie.document.getElementsByTagName("a")).length
		; if (HTMLAnchorElements[ a_index - 1 ].dataset.profileId == "1005454357")
		if (HTMLAnchorElements[ a_index - 1 ].getAttribute("data-profile-id") == "1005454357")
			MsgBox % a_index - 1
Or, maybe:

Code: Select all

ie.document.querySelector("a[data-profile-id='1005454357']")
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

Re: AHK: IE COM_getElementsByTagName

15 Jan 2019, 11:54

thanks, I'll try later.

I use inputbox in which I scan the id number, example ""1005454357",

Code: Select all

<a target="_blank" data-bind="text:LastName() + ' ' + FirstName()+ ' ' + SecondName(), attr:{'data-profile-id':$data.TicketID, 'href': '/********/SearchVisitors/VisitorInfo' + '?profileId=' + $data.ProfileID()}" data-profile-id="1005454357" href="/**********/SearchVisitors/VisitorInfo?profileId=2413703">Family Name</a>
How do i click to "href="/**********/SearchVisitors/VisitorInfo?profileId=2413703"" ?
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: AHK: IE COM_getElementsByTagName  Topic is solved

15 Jan 2019, 12:17

Use the click method upon the matching element, if any:

Code: Select all

InputBox, var
HTMLAnchorElements := ie.document.getElementsByTagName("a")
loop % HTMLAnchorElements.length {
	if (HTMLAnchorElements[ a_index - 1 ].getAttribute("data-profile-id") == var) {
		matchingElement := HTMLAnchorElements[ a_index - 1 ]
		matchingElement.click()
	}
}
rommmcek
Posts: 674
Joined: 15 Aug 2014, 15:18

Re: AHK: IE COM_getElementsByTagName

17 Jan 2019, 03:13

@A_AhkUser: I didn't make clear enough that

Code: Select all

loop, % wb.document.getElementsByTagName("INPUT").length
	 if wb.document.getElementsByTagName("INPUT")[A_Index-1].value == "Login"
		wb.document.getElementsByTagName("INPUT")[A_Index-1].click()
works for Ahk Login site, and I didn't write it. (just copied it long ago from the old forum)
But yes my pseudo attempt to help inseption86 is badly wrong. I deducted it from his ie.document.getElementsByTagName("TD")[507].innerText := "1005454357" and combined it with the one above.
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 08:14

@A_AhkUser: Thank you very mach. But, IF var missing?

Code: Select all

InputBox, var
HTMLAnchorElements := ie.document.getElementsByTagName("a")
loop % HTMLAnchorElements.length {
	if (HTMLAnchorElements[ a_index - 1 ].getAttribute("data-profile-id") == var) {
		matchingElement := HTMLAnchorElements[ a_index - 1 ]
		matchingElement.click()
	}
	else
	{
	msgbox, Missing
	}
}
is not working
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 09:57

If I understand you correctly what you need is to put your else body code after your code has gone through the whole collection, and execute it if none element could be found - for exemple:

Code: Select all

; ...
HTMLAnchorElements := ie.document.getElementsByTagName("a") ; you can get this reference once and for all if you intend to use it many times such as in a subroutine
return

!i:: ; subroutine
InputBox, var ; get user input
matchingElement := false ; it is initially assumed that the element couldn't be found
loop % HTMLAnchorElements.length {
	if (HTMLAnchorElements[ a_index - 1 ].getAttribute("data-profile-id") == var) {
		matchingElement := HTMLAnchorElements[ a_index - 1 ]
		Gosub, onMatch
	return ; exits the loop and the subroutine (use return/break - depending on the situation - if only one element is supposed to match or if you want to exit the subroutine/loop at the first match)
	}
}
if not (matchingElement) ; otherwise, if none element could be found after going through the whole collection...
	MsgBox, Missing
return

onMatch:
MsgBox % matchingElement.outerHTML
; matchingElement.click()
return
Hope this helps.
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 10:42

@A_AhkUser: Thank you very mach.


Is it possible to bypass the error "while ie.readyState! = 4" ? If run from under the admin, then everything is OK, if User - not working!

Code: Select all

ie := ComObjCreate("InternetExplorer.Application")
ie.Visible := true
ie.Navigate(URL)

while  ie.readyState!=4
	continue
burque505
Posts: 1020
Joined: 22 Jan 2017, 19:37

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 10:49

@inseption86, you could try putting the code below at the beginning of your script. It's in the docs here.. It often works.

Code: Select all

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try ; leads to having the script re-launching itself as administrator
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 10:57

@A_AhkUser: I did not admin password
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: AHK: IE COM_getElementsByTagName

19 Jan 2019, 17:51

@rommmcek
Yes, this is what I had figured out - the point was more to insist that, as I see it it, this a priori can't work since this use the legacy-if, as already pointed out by swagfag.
To be honest, I never make such a mistake considering I'm crazy about parentheses: I use them even when they are useless; I simply cannot imagine an 'if' without its pending '(' :lol: ; I think I've got myself this habit in math class in school. As for case-sensitive comparison, it's good choice (which I took up).
inseption86 wrote:Is it possible to bypass the error "while ie.readyState! = 4" ?
What kind of error? You're notified of COM errors by a MsgBox? The script get stucked at this line (it 'continues')? You can't even run IE?
Btw I use the following:

Code: Select all

while (ie.busy || ie.readyState <> 4)
   Sleep 100
inseption86
Posts: 71
Joined: 19 Apr 2018, 00:24

Re: AHK: IE COM_getElementsByTagName

20 Jan 2019, 00:05

@A_AhkUser: Error 0x80010108 on line "while (ie.busy || ie.readyState <> 4)", script is not continue, ie run and all! But if i begin script with admin user - all OK, but I want to run from the regular account
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: AHK: IE COM_getElementsByTagName

20 Jan 2019, 09:04

inseption86 wrote:
20 Jan 2019, 00:05
:arrow: COM error: 0x80010108

Hope this helps

Return to “Ask For Help”

Who is online

Users browsing this forum: clarsondd, Getfree, Google [Bot], kczx3, Lorenz, PENpal, QL_714, suoabb, tmplinshi and 48 guests