Cannot use a variable inside getElementById

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ihsanh
Posts: 4
Joined: 20 Aug 2017, 04:42
Contact:

Cannot use a variable inside getElementById

20 Aug 2017, 05:42

Maybe I am missing a very simple thing, but I cannot use a variable inside getElementById(variable).click() no matter what I do. I tied quotation marks, percentages, whatever, but it didn't work.

So, the line without the variable :

Code: Select all

ie.document.getElementById("grdkonaklayan_ctl02_Sil").click()
works, but

Code: Select all

ie.document.getElementById(idstrg_appended).click()
where idstrg_appended is equal to "grdkonaklayan_ctl02_Sil", doesn't work.

Here is the full code:

Code: Select all

 spanTags := ie.document.getElementsByTagName("span")
    s := spanTags.Length
   Loop % s 
   If (spanTags[A_Index-1].InnerText = OdaNo) {                                      ; OdaNo is a variable coming from previous code
    idstrg := spanTags[A_Index-1].Id
    StringTrimRight, idstrg_trimmed, idstrg, 13
    idstrg_appended = %idstrg_trimmed%_Sil
    MsgBox % idstrg_trimmed                                                              ; I confirmed that I got the idstrg_trimmed right
    Sleep, 500                                                                                    ;lots of time to make sure that it is not a timing problem
    ie.document.getElementById(idstrg_appended).click()
    ; ie.document.getElementById("grdkonaklayan_ctl02_Sil").click()               but this one works!
    break
    }
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Cannot use a variable inside getElementById

20 Aug 2017, 17:57

First idea that comes to my mind is that ie.document.getElementById(idstrg_appended).click() ends up with ie.document.getElementById(grdkonaklayan_ctl02_Sil).click(), as in without the quote marks. Maybe ie.document.getElementById("""" idstrg_appended """").click() would work?
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
ihsanh
Posts: 4
Joined: 20 Aug 2017, 04:42
Contact:

Re: Cannot use a variable inside getElementById

21 Aug 2017, 14:40

Unfortunately....I get this :
....the following contains illegal character "idstring_appended""""
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Cannot use a variable inside getElementById

21 Aug 2017, 19:57

Try wrapping the variable with single quotes like this:
"'" . idstring_appended . "'"
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: Cannot use a variable inside getElementById

21 Aug 2017, 21:37

What do you get if you do MsgBox % idstrg_appended == "grdkonaklayan_ctl02_Sil" ?
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Cannot use a variable inside getElementById

22 Aug 2017, 01:08

Change this part:

Code: Select all

idstrg_appended = %idstrg_trimmed%_Sil
To this:

Code: Select all

idstrg_appended := idstrg_trimmed "_Sil"
That should work.
Guest

Re: Cannot use a variable inside getElementById

22 Aug 2017, 15:18

Thank you all, but .... none of those methods worked.

Obeeb, I get 0 with "MsgBox % idstrg_appended == "grdkonaklayan_ctl02_Sil"

But "MsgBox % idstrg_appended" right after that yields..... "grdkonaklayan_ctl02_Sil". Strange

Kczx3, the quotes do not give error, but they don't seem to be doing anything new.

Cuadrix, same for the revised syntax you proposed. It also does not change the 0 (zero) result to the question
MsgBox % idstrg_appended == "grdkonaklayan_ctl02_Sil"
that Obeeb proposed.
Guest

Re: Cannot use a variable inside getElementById

22 Aug 2017, 15:46

Hmm, upon executing code line by line, I discover this :

Error: 0x80070005 - Access is denied,

with line " MsgBox % "input id=" inputTags[A_Index-1].Id" highlighted.

It is a different loop following the initial one, but I guess it is the same reason why nothing I write works on this page:

Code: Select all

Loop % i 
    If (inputTags[A_Index-1].Id != idstrg_appended) {                           ; negative statement just to force the code line by line
	MsgBox % idstrg_appended
    MsgBox % "input id=" inputTags[A_Index-1].Id                                 ; wanted to see things from the eye of the code 
	Sleep, 200
   
Any ideas? Might this be the problem?
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Cannot use a variable inside getElementById

22 Aug 2017, 17:29

Probably because the id attribute should be all lowercase?
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: Cannot use a variable inside getElementById

22 Aug 2017, 17:47

Huh, when does capitalisation matter in Autohotkey?
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Cannot use a variable inside getElementById

22 Aug 2017, 17:50

It doesn't. It matters in JavaScript. Though it's just a thought seeing as all else has yet to succeed in this thread.
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: Cannot use a variable inside getElementById

22 Aug 2017, 18:02

Guest wrote:Obeeb, I get 0 with "MsgBox % idstrg_appended == "grdkonaklayan_ctl02_Sil"
If you get 0 it 100% means they are not the same maybe there is some hidden character you are not seeing, or do you mean that Msgbox shows "grdkonaklayan_ctl02_Sil" with the quotes?! If this is the case then you need to remove them.
do Clipboard := "@" idstrg_appended "@" and then paste your clipboard directly in a post here.

As to your other problem it's unrelated. From googling it it's mostly related to CORS but I also saw people sometimes have it when they try to install or uninstall IE or something.
kczx3 wrote:Probably because the id attribute should be all lowercase?
Com objects methods and properties are not case sensitive, nice catch but this is not the problem.
ihsanh
Posts: 4
Joined: 20 Aug 2017, 04:42
Contact:

Re: Cannot use a variable inside getElementById

23 Aug 2017, 13:12

Well, first things first, "Id" or "id" does not matter.

Obeeb, yes, I do get zero. The message box shows nothing but "0" (zero) without quotation marks.

And the clipboard test yields = "@grdkonaklayan_ctl03__Sil@" (without quotation marks)

I did another test to see how variablaes are treated. I tried to click a button on the same page by software :

Code: Select all

   var2 := btnExcel
    ie.document.getElementById("%var2%").focus()
    Sleep, 200
    send, {Enter}
    Sleep, 200
    ie.document.getElementById(var2).focus()
    Sleep, 200
    ie.document.getElementById(var2).click()
    Sleep, 200
    ie.document.getElementById("var2").focus()
    Sleep, 200
    ie.document.getElementById("var2").click()
All those attempts were in vain no need to say, the software did not click anything.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Cannot use a variable inside getElementById

23 Aug 2017, 13:54

I can confirm that the following brief example DOES work for me. I tried to mimic as close as I could to what you are doing with using both := and just =.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

ID_first := "site-"
ID_second = %ID_first%description

ie := ComObjCreate("InternetExplorer.Application")
ie.visible := true
ie.navigate("www.autohotkey.com/boards")
while ie.busy && ie.readystate != 4
	sleep 50

sleep, 3000

ie.document.getElementById(ID_second).children[0].click()

sleep, 5000

ie.quit()

ie := ""
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Cannot use a variable inside getElementById

23 Aug 2017, 14:00

Also, I don't think anyone asked this yet but is this webpage something that we can access to help test?
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: Cannot use a variable inside getElementById

23 Aug 2017, 14:37

ihsanh wrote:Obeeb, yes, I do get zero. The message box shows nothing but "0" (zero) without quotation marks.
And the clipboard test yields = "@grdkonaklayan_ctl03__Sil@" (without quotation marks)
I wasn't doubting that you get zero, I meant that if you get 0 it means the strings are different.
Now that you showed us the contents of your variable I can tell what those differences are.

"grdkonaklayan_ctl02_Sil" is what you use without variable.
"grdkonaklayan_ctl03__Sil" is the contents of your variable.

The first one has ctl02 the second ctl03, that may be intended but the second difference is the double underscore between ctl03 and Sil that is definitely not intended.
ihsanh
Posts: 4
Joined: 20 Aug 2017, 04:42
Contact:

Re: Cannot use a variable inside getElementById

24 Aug 2017, 23:44

"grdkonaklayan_ctl02_Sil" is what you use without variable.
"grdkonaklayan_ctl03__Sil" is the contents of your variable.
Yes, Obeeb, that is it, finally worked! It is my carelessness of course. Many thanks to everyone who has contributed. You are invaluable.

And the working code is :

Code: Select all

ie.document.getElementById(idstrg_appended).focus()
    Sleep, 200
    ie.document.getElementById(idstrg_appended).click()
	Sleep, 1500
The following does not seem to work :

Code: Select all

 ie.document.getElementById(%idstrg_appended%).focus()
    Sleep, 200
    ie.document.getElementById(%idstrg_appended%).click()
    send, {Enter}
    Sleep, 200
    ie.document.getElementById("' . idstrg_appended . '").focus()
    Sleep, 200
    ie.document.getElementById("' . idstrg_appended . '").click()
I don't know why some experiments I did, like the one with the var2 variable and button did not work, it might be another mistake, or that autohotkey needed a reset. Because I realised that it was working strangely before the reset.

Many thanks again

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, demon740, mapcarter and 297 guests