Page 1 of 1

AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 08:45
by inseption86
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>

Re: AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 10:04
by rommmcek
Maybe:

Code: Select all

loop, % ie.document.getElementsByTagName("TD").length
	 if ie.document.getElementsByTagName("TD")[A_Index-1].value == "1005454357"
		MsgBox % A_Index-1

Re: AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 10:45
by swagfag
legacy-if + case-sensitive comparison, what a disaster. ure comparing var to an equal sign =

Re: AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 11:20
by rommmcek
@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!

Re: AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 11:27
by A_AhkUser
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']")

Re: AHK: IE COM_getElementsByTagName

Posted: 15 Jan 2019, 11:54
by inseption86
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"" ?

Re: AHK: IE COM_getElementsByTagName  Topic is solved

Posted: 15 Jan 2019, 12:17
by A_AhkUser
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()
	}
}

Re: AHK: IE COM_getElementsByTagName

Posted: 17 Jan 2019, 03:13
by rommmcek
@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.

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 08:14
by inseption86
@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

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 09:57
by A_AhkUser
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.

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 10:42
by inseption86
@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

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 10:49
by burque505
@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
}

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 10:57
by inseption86
@A_AhkUser: I did not admin password

Re: AHK: IE COM_getElementsByTagName

Posted: 19 Jan 2019, 17:51
by A_AhkUser
@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

Re: AHK: IE COM_getElementsByTagName

Posted: 20 Jan 2019, 00:05
by inseption86
@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

Re: AHK: IE COM_getElementsByTagName

Posted: 20 Jan 2019, 09:04
by A_AhkUser
inseption86 wrote:
20 Jan 2019, 00:05
:arrow: COM error: 0x80010108

Hope this helps