Chrome.ahk Help; Can't Fill In Text Box

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tatleoat
Posts: 24
Joined: 16 Mar 2021, 14:54

Chrome.ahk Help; Can't Fill In Text Box

Post by tatleoat » 18 Jun 2021, 15:01

So I'm using the chrome.ahk library and I'm trying to make a script that automatically fills in a search bar but it doesn't work, so far the script is as follows:

Code: Select all

^F1::
TabInstance := Chrome.GetPage()
TabInstance.Evaluate("alert('hi!');")
TabInstance.Evaluate("document.getElementById('txtSearch').value = '123456'")
return
And the code for the search box looks like this:

Code: Select all

<input name="txtSearch" type="text" id="txtSearch" class="bodytext" style="height:19px;width:100px;">
And this is the error message I get when I try to use this script:
image.png
image.png (25.54 KiB) Viewed 1926 times
Any advice would be greatly appreciated, I can get the same thing working on Jacks AHK Chrome Extension tester page but not with a different page. Thanks in advance.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by gregster » 18 Jun 2021, 19:01

I don't see any obvious problem - code looks functional.

The thing that confuses me is the error message, saying "Cannot read property 'click' ". First of all, click probably shouldn't be a property (it's usually a method that simulates a mouseclick on an element), and you are actually trying to set a (different) property in your code; besides, there is no click of any kind in it...

That's why I have doubts that the code above actually accounts for this error message. Please check again, and look for any clicks you have anywhere in the rest of your code.

tatleoat
Posts: 24
Joined: 16 Mar 2021, 14:54

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by tatleoat » 18 Jun 2021, 19:19

I'm sorry, I must have accidentally sent a screencap from when I was attempting some different things with the code that I thought might work and forgot to change it back for this post. Usually it says 'value' where it says 'click' but everything else is the same I think.

If the code looks functional though then I'm just as confused as you are. Is there any other information you'd like to know that might narrow down what the problem might be?

EDIT: Here's also more of the code above the search bar.
image.png
image.png (73.33 KiB) Viewed 1879 times

tatleoat
Posts: 24
Joined: 16 Mar 2021, 14:54

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by tatleoat » 18 Jun 2021, 19:27

Here's a more up to date screencap in case you spot something:
image.png
image.png (23.4 KiB) Viewed 1879 times

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by gregster » 18 Jun 2021, 20:46

My guess, looking at your screenshot: the input element is in a different js context, a frame:

frame.png
frame.png (24.16 KiB) Viewed 1859 times

That complicates things. Can you identify the (i)frame ID or name?
I think frames (without i) are outdated, but you might still encounter them...

If it's a frame, you can probably do something like this (replace the frame name):

[bad code removed so that people won't try it]

(untested - this is more a js than AHK question)


If it's an iframe - then you can usually do something like this:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890&p=371777#p371777
Last edited by gregster on 19 Jun 2021, 11:19, edited 4 times in total.
Reason: Bad code removed

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by teadrinker » 18 Jun 2021, 20:50

tatleoat wrote:

Code: Select all

^F1::
TabInstance := Chrome.GetPage()
TabInstance.Evaluate("alert('hi!');")
TabInstance.Evaluate("document.getElementById('txtSearch').value = '123456'")
return
Is this whole code? Look at the first example in this post:

Code: Select all

#Include Chrome.ahk

; Create an instance of the Chrome class using
; the folder ChromeProfile to store the user profile
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile")
You must first create an instance of the class Chrome. The variable your instance is stored in can't be named Chrome because you are overwriting the class variable.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by gregster » 18 Jun 2021, 21:51

But would we even get to the error message above, if this would be the problem in this case? :think:

tatleoat
Posts: 24
Joined: 16 Mar 2021, 14:54

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by tatleoat » 19 Jun 2021, 06:59

Thank you both for your efforts in helping me out with this, it's really appreciated and has given me some food for thought for getting this to work properly but so far nothing has really worked. I think you're correct in that it has something to do with it being embedded in a frame; here's the code above the other screencap I sent you with the part about the frame highlighted:
image.png
image.png (24.99 KiB) Viewed 1786 times
Here is the totality of the code I'm using to test this particular function out:

Code: Select all



#Include C:\Users\Tristan.Jonas\Desktop\AHK Scripts\LIBRARIES\Chrome.ahk


^F2::
TabInstance := Chrome.GetPage()
iframeJs =
(
var iframe = document.getElementById('topFrame');
iframe.contentDocument.getElementById('txtSearch').value = '123456';
)
PageInstance.Evaluate(iframeJs)
return


^F3::
TabInstance := Chrome.GetPage()
frameJs =
(
var aframe = document.parentWindow.frames['topFrame']; 
aframe.document.getElementById('txtSearch').value = '123456';
)
TabInstance.Evaluate(frameJs)
return

^F4::
TabInstance := Chrome.GetPage()
TabInstance.Evaluate("document.getElementById('txtSearch').value = '123456'")
return

^F5::
 FileCreateDir, ChromeProfile
 ChromeInst := new Chrome("ChromeProfile")
return
I'm using ^F5 to open up a window that's 'connected' to the code and everything else to test different possibilities. The only other thing of note is that when I use ^F3 I get this error message that's slightly different than before:
image.png
image.png (23.56 KiB) Viewed 1786 times
I'm sorry that I'm almost at this point asking you to do this for me, I'm just at my wits end trying to figure something out that should ostensibly be extremely simple. If you're stumped maybe you can point me in a direction of a more JS focused resource that might be able to help?

Thank you so much.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by gregster » 19 Jun 2021, 11:40

tatleoat wrote:
19 Jun 2021, 06:59
I'm sorry that I'm almost at this point asking you to do this for me, I'm just at my wits end trying to figure something out that should ostensibly be extremely simple. If you're stumped maybe you can point me in a direction of a more JS focused resource that might be able to help?
I wouldn't call the handling of frames "extremely simple" - they can be a bit tricky, expecially if you don't have the page available for testing. (I assume it's something you can't/don't want to share ? If we had that URL for testing, it would simplify things a lot.) It's perhaps once or twice a year I deal with frames, and I always have to recall how it's done. It helps immensely to be able to check the javascript in the browser console, before adding it to your Chrome.ahk script.

Yesterday, I tried to give you some code without being able to test anything - and I think my js syntax was not correct, and I removed it now from my post above (so that future users won't try it unsuccessfully).


So we are looking at a frame named 'topFrame' (it's a Name, the frame doesn't have an Id in this case - we have to account for that).
Also, if you create a Chrome instance named ChromeInst (ChromeInst := new Chrome("ChromeProfile")), you should also use that instance in TabInstance := ChromeInst.GetPage()

So, let's try this:

Code: Select all

#NoEnv
#Include Chrome.ahk
SetBatchLines, -1

ChromeInst := new Chrome("ChromeProfile")

; now navigate to your page


^F3::
TabInstance := ChromeInst.GetPage()
; names are not unique, but let's assume a frame has the index 0
frameJs =
(
var aframe = document.getElementsByName('topFrame')[0];			
aframe.contentDocument.getElementById('txtSearch').value = '123456';
)
TabInstance.Evaluate(frameJs)
return

Esc::ExitApp
(js syntax should hopefully be better now ;) )
Last edited by gregster on 19 Jun 2021, 12:48, edited 1 time in total.
Reason: fix

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by safetycar » 19 Jun 2021, 12:28

If it's worth something of what I remember...
Dealing with frames can be quite tricky. There can be privacy protections in the way of certain actions.
Also I'm not sure if you are aware that frames have it's own window object.
So once you get the frame you have to access it's window and start treating it as if was a window.
Here's a property to access the window of an <iframe>, which I think it should work for <frame> but I'm not really sure:
https://www.w3schools.com/jsref/prop_frame_contentwindow.asp
And you can expect to have to deal with problems like:
https://stackoverflow.com/questions/36333978/error-permission-denied-to-access-property-document

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by safetycar » 19 Jun 2021, 12:34

But that said... If what you're trying to do is simple enough...
if you go and take the src attribute of the frame it's likely that you can open that in a separate window and can work from there?
Last edited by safetycar on 19 Jun 2021, 12:46, edited 1 time in total.

tatleoat
Posts: 24
Joined: 16 Mar 2021, 14:54

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by tatleoat » 19 Jun 2021, 12:40

It works!!! But only after I changed one tiny thing, surprisingly:

Code: Select all

#NoEnv
#Include C:\Users\Tristan.Jonas\Desktop\AHK Scripts\LIBRARIES\Chrome.ahk
SetBatchLines, -1

ChromeInst := new Chrome("ChromeProfile")

; now navigate to your page


^F3::
TabInstance := ChromeInst.GetPage()
framejs = 
(
var aframe = document.getElementsByName('topFrame')[0]
aframe.contentDocument.getElementById('txtSearch').value = '123456'
)
TabInstance.Evaluate(framejs)
return

Esc::ExitApp
the "; ; names are not unique, but let's assume a frame has the index 0" note believe it or not confused the script, I don't think I've ever seen notes do that before but I had a feeling AHK might have thought of it as like being in the middle of the code, as weird as that sounds. Once I took it out it worked perfectly! In case anyone else comes across this problem this is what the error message looked like ("Unexpected Identifier") and might indicate something similar:
image.png
image.png (22.78 KiB) Viewed 1732 times
Thank you so much for your patience and help, it must have been frustrated and I wish I could have just sent you the link directly but the search bar was through company software that you have to be logged in to view with a company account.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Chrome.ahk Help; Can't Fill In Text Box

Post by gregster » 19 Jun 2021, 12:46

tatleoat wrote:
19 Jun 2021, 12:40
believe it or not confused the script, I don't think I've ever seen notes do that before but I had a feeling AHK might have thought of it as like being in the middle of the code, as weird as that sounds. Once I took it out it worked perfectly!
I believe it - it was something I added later for clarification without realising that i added it as part of the js code :facepalm: :) - and js has different syntax/comment rules than AHK.

Glad that you figured it out :thumbup: (I fixed it now in my post above - thank you for the feedback!)

Post Reply

Return to “Ask for Help (v1)”