Interacting with IE with COM Interface and Java Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Interacting with IE with COM Interface and Java Script

20 Sep 2019, 09:19

I have noticed that in some websites I can't interact with some of the elements while with other elements in the same website I have no problem. All have attributes visible with an 'Inspect'

I would prefer to stay with IE since I will mix COM Interface and Java script

In the first example https://admin.typeform.com/signup website I can fill all fields with information while i can't click on the SEND button even if it is visible with an 'Inspect'.
(The 2 last lines are there 'cause I am trying different options, I execute only one of them, naturally)

Code: Select all

If !IsObject(ie)
	ie := ComObjCreate("InternetExplorer.Application")
ie.Visible := true
ie.Navigate("https://admin.typeform.com/signup")
Sleep, 500
IELoad(ie)
WinMaximize, Sign up FREE | Typeform - Internet Explorer
Sleep, 500
Sleep, 500
IE.document.getElementById("name").Value := "Mary"  ; INSERT USER ID
Sleep, 500
IE.document.getElementById("email").Value := "hello@abc.se"  ; INSERT USER ID
Sleep, 500
IE.document.getElementById("password").Value := "Abc123"  ; INSERT USER ID
Sleep, 500
IE.document.getElementById("terms").Click()  ; INSERT USER ID
Sleep, 500
IE.document.getElementById("marketing").Click()  ; INSERT USER ID
Sleep, 500
IE.document.getElementByClass("button__Button-sc-18qod6g-0 eIOrzB").Click()  ; INSERT USER ID
Sleep, 500
IE.document.getElementsByClassName('btn button__Button-sc-18qod6g-0 eIOrzB')[1].Click()  ; INSERT USER ID
Sleep, 500
In the second example http://hem.stamford.se/fylliettformular?lang=en I can see the fields attributes with an 'Inspect' but I cannot write to them or at least Java Script does not react.
Also I cannot see the attributes of the fields with a 'Spy' to use Com Interface.

ALSO: How can I see the SEND ("Skicka") button? I can't do an IE 'Inspect' on it.

Code: Select all

If !IsObject(ie)
	ie := ComObjCreate("InternetExplorer.Application")
ie.Visible := true
ie.Navigate("http://hem.stamford.se/fylliettformular?lang=en")
Sleep, 500
IELoad(ie)
WinMaximize, Fyll i ett formulär | Stamford - Internet Explorer
Sleep, 500
Send, {PgDn}
Sleep, 1000
IE.document.getElementById("contact[firstName]").Value := "Mary"  ; INSERT USER ID
Thanks in advance.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Interacting with IE with COM Interface and Java Script

20 Sep 2019, 11:00

As far as I know, there isn't a method called getElementByClass, so this line will never work:
IE.document.getElementByClass("button__Button-sc-18qod6g-0 eIOrzB").Click()

This other line can work if you fix the class name, like this:
IE.document.getElementsByClassName('button__Button-sc-18qod6g-0')[1].Click()

The space character in the class attribute is a separator for different class names. You have to pass only one class name to getElementsByClassName. But you were passing 3 class names, that wouldn't work.

In your second example, the last line should be:
IE.document.getElementsByName("contact[firstName]")[0].value := "Mary"
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

20 Sep 2019, 12:48

Getfree wrote:
20 Sep 2019, 11:00
As far as I know, there isn't a method called getElementByClass, so this line will never work:
IE.document.getElementByClass("button__Button-sc-18qod6g-0 eIOrzB").Click()

This other line can work if you fix the class name, like this:
IE.document.getElementsByClassName('button__Button-sc-18qod6g-0')[1].Click()

The space character in the class attribute is a separator for different class names. You have to pass only one class name to getElementsByClassName. But you were passing 3 class names, that wouldn't work.

In your second example, the last line should be:
IE.document.getElementsByName("contact[firstName]")[0].value := "Mary"
Thanks a lot, it works.

I have a field where I must enter userid.

https://postimg.cc/NyCwR7Sv

Have tried the options below:

Code: Select all

document.getElementsByClassName("inline")[1].value := "Mary"

document.getElementsByClassName("inline")[0].value := "Mary"

document.getElementsByName("USERNAME.DUMMY.DUMMY.1")[0].value := "Mary"

document.getElementsByName("USERNAME.DUMMY.DUMMY.1")[1].value := "Mary"

document.getElementById("namedata").value := "Mary"
What should I use?
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Interacting with IE with COM Interface and Java Script

20 Sep 2019, 13:51

Based on the HTML I see in that screenshot, any of these should work:

Code: Select all

document.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
document.getElementsByName("USERNAME.DUMMY.DUMMY.1")[0].value := "Mary"
If they don't work, a possible cause is that the field is inside a frame or that there are other fields with the same name or ID.
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

20 Sep 2019, 14:20

Getfree wrote:
20 Sep 2019, 13:51
Based on the HTML I see in that screenshot, any of these should work:

Code: Select all

document.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
document.getElementsByName("USERNAME.DUMMY.DUMMY.1")[0].value := "Mary"
If they don't work, a possible cause is that the field is inside a frame or that there are other fields with the same name or ID.
Thanks. Unfortunately none of them worked. Include all the code in case it helps.

I will have to do the same query for the password field:

Code: Select all

var newloginhtml = '<img src="../../kmdatrium/images/login/Login_background_kmd.jpg" class="bg"><div align=center valign=middle class="login-center-transparent">\
	<div class="login-top-left">\
		<div class="container">\
		<span [b]class="password[/b]"><div class="inline" id="usernametext"></div>&nbsp;&nbsp;</span><div class="inline" id="namedata"><X-SUBST type="text" name="USERNAME.DUMMY.DUMMY" class="form" autocomplete="Off"></X-SUBST></div>\
		<br>\
		<span class="password"><div class="inline" id="pwdtext"></div>&nbsp;&nbsp;</span><div class="inline" id="pwddata"><X-SUBST type="text" name="PASSWORD.DUMMY.DUMMY" class="form" autocomplete="Off"></X-SUBST></div>\
		<br>\

and the login button

Code: Select all

		<div class="loginbutton" id="submitdata">\
			<X-SUBST type="text" name="ATTEMPT.DUMMY.DUMMY" class="form"></X-SUBST>\
			<X-SUBST type="submit" name="LOGIN.DUMMY.DUMMY" src="../../generic/images/login/Logo_for_phase_1_kmd.jpeg"><INPUT type="submit" name="LOGIN.DUMMY.DUMMY" value="Click Here to Login"></X-SUBST>\
		</div>\
HTML Code:

Code: Select all

<!DOCTYPE html>
<html><!-- Arhettan Startup Screen --><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="CACHE-CONTROL" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Arhettan On The Web</title>
<style type="text/css">
HTML,BODY {
	/* The styles on the body element here prevent IE form showing a greyed-out vertical scroll bar on the browser window at all times. */
	width: 100%;
	height: 100%;
	overflow: auto;
}

IFRAME {
	width: 100%;
	height: 100%;
	position: absolute;
}
</style>
<script type="text/javascript">
var appBodyClassName="SKINNED";
var newlogincss = '../../kmdnatrium/login_kmd1.css';
var customBranding = {'welcomeURL':'../../kmdnatrium/kmdnatrium.html'};

var newloginhtml = '<img src="../../kmdnatrium/images/login/Login_background_kmd.jpg" class="bg"><div align=center valign=middle class="login-center-transparent">\
	<div class="login-top-left">\
		<div class="container">\
		<span class="password"><div class="inline" id="usernametext"></div>&nbsp;&nbsp;</span><div class="inline" id="namedata"><X-SUBST type="text" name="USERNAME.DUMMY.DUMMY" class="form" autocomplete="Off"></X-SUBST></div>\
		<br>\
		<span class="password"><div class="inline" id="pwdtext"></div>&nbsp;&nbsp;</span><div class="inline" id="pwddata"><X-SUBST type="text" name="PASSWORD.DUMMY.DUMMY" class="form" autocomplete="Off"></X-SUBST></div>\
		<br>\
		<span>\
		<div class="rememberme">\
		<div class="inline" id="remembertext"></div>\
		<div class="inline" id="rememberdata"><X-SUBST type="checkbox" name="REMEMBER_ME.DUMMY.DUMMY" autocomplete="Off"></X-SUBST></div>&nbsp;\
		</div>\
		<div class="loginbutton" id="submitdata">\
			<X-SUBST type="text" name="ATTEMPT.DUMMY.DUMMY" class="form"></X-SUBST>\
			<X-SUBST type="submit" name="LOGIN.DUMMY.DUMMY" src="../../generic/images/login/Logo_for_phase_1_kmd.jpeg"><INPUT type="submit" name="LOGIN.DUMMY.DUMMY" value="Click Here to Login"></X-SUBST>\
		</div>\
		</span>\
		</div>\
	</div>\
	<div class="login-top-right">\
		<div class="img">\
		</div>\
	</div>\
	<div class="login-bottom">\
		<div id="MESSAGEBOARD"></div>\
		<div id="loginLinkContainer" style="float:left">\
		</div>\
		<div class="login-bottom-info">\
		+45 44 605 722 <b>|</b> <a href="mailto:natriumsupport@kmd.tu">natriumsupport@kmd.tu</a> <b>|</b> <a href="www.kmd.tu">www.kmd.tu</a>\
		</div>\
		<div class="login-version-text" id="versiontext"></div>\
	</div>\
</div>';

if(top != window){
	top.location = window.location.href;
}

function getWindowState(){var g=this,f={scrollLeft:NaN,scrollTop:NaN},j={clientWidth:NaN,clientHeight:NaN},k={innerWidth:NaN,innerHeight:NaN},l='scrollLeft',m='scrollTop',n=0,p,q,r,s,u=null,v=null, o={getScrollX:x,getScrollY:y,getWidth:function(){return I('getWidth');},getHeight:function(){return I('getHeight');}};function X(){J();return x();}function Y(){J();return q;}function x(){return f[l]|0;}function y(){return f[m]|0;}function W(){return k.innerWidth;}function w(){return j.clientWidth;}function H(){return k.innerHeight;}function h(){return j.clientHeight;}function U(){return(F()?o.getHeight:h)();}function V(){return (F()?o.getWidth:w)();}function D(){o.getWidth=W;o.getHeight=H;}function E(){o.getScrollX=x;o.getScrollY=y;}function F(){if((s!=(s=H()))||(r!=(r=W()))){R();return true;}return false;}function J(){if((q!=(q=y()))||(p!=(p=x()))){R();}}function K(a,b){var V,H;if(((V=(g.innerHeight-a.clientHeight))>=0)&&((H=(g.innerWidth-a.clientWidth))>=0)&&(!(x()&&!V))&&(!(y()&&!H))&&(!((H&&(b.h||(b.h=H))&&(H!=b.h))||(V&&(b.v||(b.v=V))&&(V!=b.v))))){if(H==V){b.r=(+Boolean(H));}else if((H&&!V)||(V&&!H)){b.r=(H+V);}else{b.r=NaN;}}else{b.r=NaN;}return b;}function P(t){for(var c=arguments.length;--c;){if(typeof t[arguments[c]]!='number'){return false;}}return true;}function Q(a,b){if(a&&P(a,'clientWidth','clientHeight')){K(a,b);}return b;}function I(c){r=W();s=H();q=y();p=x();u={v:0,h:0,r:NaN};v={v:0,h:0,r:NaN};o.getWidth=V;o.getHeight=U;o.getScrollX=X;o.getScrollY=Y;R();Q=K;return o[c]();}function R(){var c=document,b=isNaN(Q(c.body,u).r),d=isNaN(Q(c.documentElement,v).r);if(b||d){if(b&&d){E();D();}else{j=((d)?c.body:c.documentElement);u=((d)?u:v);v=null;R=T;}}else{j=((u.r < v.r)?c.body:c.documentElement);}}function T(){if(isNaN(Q(j,u).r)){u=null;E();D();}else if((u.v)&&(u.h)&&(++n > 2)){u=null;o.getWidth=w;o.getHeight=h;E();}}if(!P(g,'innerHeight','innerWidth')){j=compatModeTest(j);o.getWidth=w;o.getHeight=h;}else{k=g;}if(P(g,'pageYOffset','pageXOffset')){f=g;m='pageYOffset';l='pageXOffset';}else{f=compatModeTest(f);}return(getWindowState=function(){return o;})();}
function compatModeTest(o){var d=document;if((d.compatMode)&&(d.compatMode.indexOf('CSS')!=-1)&&(d.documentElement)){return (compatModeTest=function(){return d.documentElement;})((o=null));}else if(d.body){return (compatModeTest=function(){return d.body;})((o=null));}else{return o;}}
</script>
</head>
<body topmargin="0" marginwidth="0" marginheight="0" leftmargin="0">
	<div style="position: absolute; bottom: 0px; background-color: #888888; width: 100%; padding: 0px; margin: 0px;">
		<select id="messageBox" style="width: 100%; padding: 0px; margin: 0px; border: 0px none #888888;">
			<option selected="selected"></option>
			<option></option>
			<option></option>
			<option></option>
		</select>
	</div>
	<script language="JAVASCRIPT" src="natriumkmd_files/Arhettan.js"></script><iframe name="application" id="application" marginheight="0" marginwidth="0" src="natriumkmd_files/SPDEMLOGIN.htm" width="100%" height="100%" frameborder="0"></iframe>


</body></html>
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Interacting with IE with COM Interface and Java Script

20 Sep 2019, 18:25

Apparently the HTML is being generated after the page loads. And I see an <iframe> in there too.
It's not possible to figure out the correct selector with the original HTML.

You can inspect that specific element with Chrome's DevTools and choose "Copy selector" in its context menu (right mouse button).
That will give you a working CSS selector that you can use with document.querySelector.
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

25 Sep 2019, 05:48

Getfree you were right, the field is inside a frame.
I have found the solution and it works fine in Chrome Console.

Code: Select all

var iframevar = document.getElementById("application")  ; 
var innerDoc = iframevar.contentDocument || iframe.contentWindow.document  ;
innerDoc.getElementById("USERNAME.DUMMY.DUMMY").value = "Mary";
Note: in Chrome Console it only works with "=" not with ":="

However. I cannot succeed to make this work in AutoHotKey.
Can't tell if it is the equal signs or what.

Line 1) Using ":=" in AHK throws an error
Line 2) Using ":=" in AHK throws an error
Line 3) Using ":=" in AHK does not throw an error. However the field is not populated while it does in Chrome Console.

Can someone help how should I put this in AHK?
Should it always be in 3 lines? Can it be reduced to one line, all three actions in one line?
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 05:41

Really? Nobody can tell me how to implement in AHK those 3 JavaScript rows that are working fine in Chrome Console?
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 09:23

Obviously, you can't just throw javascript lines into an AHK script. So, I would at least adjust these syntax elements for the use with AHK/Com IE:
  • remove the javascript var statement - it doesn't exist in AHK.
  • remove trailing ;s - at least, if there is no space between code and the ; (yes, I am looking at you, Mary: "Mary"; )
  • use := for assigments, not =

But I have concerns about this original line: var innerDoc = iframevar.contentDocument || iframe.contentWindow.document

What does it exactly do in javascript? It supposes that only one of these elements exists and then assigns it to the variable?
I doubt that this will work in AHK... you will probably just get 0 or 1 (false or true)

Perhaps rather this - but untested:

Code: Select all

innerDoc := iframevar.contentDocument ? iframevar.contentDocument : iframe.contentWindow.document
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:20

You are right gregster

Code: Select all

|| iframe.contentWindow.document
is wrong it should be

Code: Select all

|| iframevar.contentWindow.document
However it seems like Console cared only about

Code: Select all

iframevar.contentDocument
and ignored

Code: Select all

|| iframe.contentWindow.document
Anyhow, I have now reduced the three code lines to just one, this works lovely in Console ....

Code: Select all

document.getElementById("application").contentDocument.getElementById("USERNAME.DUMMY.DUMMY").value = "Mary"
.... but still no success in AHK (with ":="):

Code: Select all

document.getElementById("application").contentDocument.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
Any ideas how I could implement this in AutoHotKey would be highly appreciated. Thanks in advance.
Last edited by aircooled on 26 Sep 2019, 11:26, edited 1 time in total.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:25

For sure, you will have to use := in AHK, like I mentioned above.
(It would be different, if you would actually execute a javascript code line).
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:27

You right again gregster, just corrected and tested. Same story.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:34

Not sure...
But the AHK code with multiple lines did work? Or did it fail, too?
I would first concentrate to make it work on multiple lines (then it's easier to find a problem) - and then you could still make it into a single line...

But sorry, I don't have enough experience with iframes that I could tell you much without actually having the possibility to try it out.
Perhaps, if I have time, I can look for and look at some page with iframes later, but perhaps someone sees something that I overlooked so far.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:38

Oh yeah, and of course you should make reference to the actual COM object, for example:

Code: Select all

ie.document.getElementById("application").contentDocument.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
So add ie or whatever you called the IE COM object when you created an instance of the COM interface (like ie := ComObjCreate("InternetExplorer.Application") )

That would be implied in the console of a specific webpage, but your AHK script can't know what object's document property it should use without you telling it.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Interacting with IE with COM Interface and Java Script

26 Sep 2019, 11:44

Assuming your web browser control is in the variable wb, any of these two lines should work:

wb.document.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"

wb.document.parentWindow.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

27 Sep 2019, 01:04

Getfree wrote:
26 Sep 2019, 11:44
Assuming your web browser control is in the variable wb, any of these two lines should work:

wb.document.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"

wb.document.parentWindow.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
Thanks Getfree. I have now qualified with web browser control ie

Scenario 1: Your ex. #1 and getElementById:
ie := ComObjCreate(("InternetExplorer.Application"))
ie.Visible:=True
ie.Navigate("https://xxxx/xxxx/man.htm")
ie.document.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"

Error:
https://postimg.cc/GBfz2LkH

Scenario 2: Your ex. #1 with square brackets around "application":
ie.document.frames("application").getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
Error:
https://postimg.cc/xk9qzM3Z

Scenario 3: Your ex. #2 and getElementById:
ie.document.parentWindow.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
Error:
https://postimg.cc/fJ4BSkYn

Scenario 4: Your ex. #2 and getElementByName:
ie.document.parentWindow.frames["application"].getElementByName("USERNAME.DUMMY.DUMMY.1").value := "Mary"
Error:
https://postimg.cc/c6Vsyw2n

HTML code when inspecting username field:
https://postimg.cc/567DWV6k
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Interacting with IE with COM Interface and Java Script

27 Sep 2019, 02:51

Script issues are easier to help with if we see all of your code and if we have access to the thing giving you issues

in your case one line is most likely not the only part of the problem so maybe post more of your script, also without access to your page with the framed elements it is harder to give exact help that works for all

in this topic https://autohotkey.com/board/topic/91443-comie-error-0x80070005-access-is-denied-with-paypal/ i try to help someone else having frame issues maybe it can help you too
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Interacting with IE with COM Interface and Java Script

27 Sep 2019, 08:56

gregster wrote:
26 Sep 2019, 11:38
Oh yeah, and of course you should make reference to the actual COM object, for example:

Code: Select all

ie.document.getElementById("application").contentDocument.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
So add ie or whatever you called the IE COM object when you created an instance of the COM interface (like ie := ComObjCreate("InternetExplorer.Application") )

That would be implied in the console of a specific webpage, but your AHK script can't know what object's document property it should use without you telling it.
Did you try this ?
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

28 Sep 2019, 02:00

Blackholyman wrote:
27 Sep 2019, 02:51
Script issues are easier to help with if we see all of your code and if we have access to the thing giving you issues

in your case one line is most likely not the only part of the problem so maybe post more of your script, also without access to your page with the framed elements it is harder to give exact help that works for all

in this topic https://autohotkey.com/board/topic/91443-comie-error-0x80070005-access-is-denied-with-paypal/ i try to help someone else having frame issues maybe it can help you too
Thanks a lot.
However, my whole code IS there. I am trying with only those four lines and nothing else:

ie := ComObjCreate(("InternetExplorer.Application"))
ie.Visible:=True
ie.Navigate("https://xxxx/xxxx/man.htm")
ie.document.frames["application"].getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"


In the 4 scenarios I describe above I just change line 4 to other alternatives but still only 4 lines.

Also I have included the HTML code.
https://postimg.cc/567DWV6k
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Interacting with IE with COM Interface and Java Script

28 Sep 2019, 02:02

gregster wrote:
27 Sep 2019, 08:56
gregster wrote:
26 Sep 2019, 11:38
Oh yeah, and of course you should make reference to the actual COM object, for example:

Code: Select all

ie.document.getElementById("application").contentDocument.getElementById("USERNAME.DUMMY.DUMMY").value := "Mary"
So add ie or whatever you called the IE COM object when you created an instance of the COM interface (like ie := ComObjCreate("InternetExplorer.Application") )

That would be implied in the console of a specific webpage, but your AHK script can't know what object's document property it should use without you telling it.
Did you try this ?
Yes yes, I have tried that now all statements are ie qualified.

See my message above from 7:04

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], RussF, ymnaren and 155 guests