IE automation with javascript button

by megamorf at 2012-10-18 16:27:04

Hello everybody,

I’m trying to automate uploading logfiles to the Citrix Autosupport website. You can upload logfiles for XenDesktop, XenServer, XenApp, NetScaler and it will scan them for hundreds of known issues.

My problem is that I can’t get the login button click method to work because there’s a lot of javascript involved. The website is https://taas.citrix.com/AutoSupport/ and I hope you are able to help me figure out how to do it.

$ie = new-object -com InternetExplorer.Application
$ie.Navigate("https://taas.citrix.com/AutoSupport/")
while ($ie.Busy -eq $true){Start-Sleep -Milliseconds 1000}
$ie.Visible=$true
$ie.Document.getElementById("username").value = "mycitrixusername"
$ie.Document.getElementById("password").value = "supersecretpassword"
$ie.Document.getElementById("btnLogOn").Click()


Here’s the login button
[quote]
<td align="left">
<input id="btnLogOn" type="button" style="width:230px" value="Let’s Go"/>
<input type="hidden" id="txtNext" value=""/>
</td>[/quote]

Here are the functions from the last javascript block of the page that are relevant (in my opinion):
[quote]
var logon = function() {
var err = false;
var username = $.trim(txtUsername.val());
var password = $.trim(txtPassword.val());
if (!username) {
errMessage.html("Username is required.");
err = true;
}
if (!password) {
errMessage.html("Password is required.");
err = true;
}
if (err) {
errInfo.show();
return false;
}

var data = {};
data[txtUsername.attr(‘name’)] = username;
data[txtPassword.attr(‘name’)] = password;
if (ckbRememberMe.jSym_checkbox(‘isChecked’)) {
data[‘remember_me’] = true;
}

disableFields();
if (errInfo.is(‘:visible’))
errInfo.hide();

$.ajax({
url : form.attr(‘action’),
type : form.attr(‘method’),
dataType : ‘json’,
data : data,
error : function() {
errMessage.html(‘Login not accepted. Please try again.’);
errInfo.show();
resetFields();
},
success : function(res) {
if (res.result == ‘Ok’) {
_gaq.push([‘_trackEvent’, ‘Logon’, ‘Clicked’, ‘Homepage’]);
setTimeout(function () {
if (next && next != location.href) {
location.href = next;
} else {
location.reload(true);
}
}, 100);
} else {
errMessage.html(res.result ? res.result : ‘Invalid login. Please try again.’);
errInfo.show();
resetFields();
}
}
});
return false;
};

var btnLogon = $(‘#btnLogOn’).jSym_button({
buttonType : ‘cta’,
buttonClick : logon
});

var form = $(‘#frmLogin’).keypress(function(evt) {
if (evt.which == 13) {
logon();
return false;
}
});
[/quote]
by DonJ at 2012-10-19 09:46:43
I’ve run into this a number of times, and have been told by folks smarter than me that it’s a limitation in the IE COM interface. In other words, it won’t do it. You could look into something like AutoIt to automate actually clicking the button with the mouse pointer.
by megamorf at 2012-10-19 13:33:49
Okay, to sum this up. I wasn’t able to automate the process because there’s way too much javascript involved after you logged in.

However, I managed to login by using an automation module called wasp

My code looks like this for the login process. That should work where there’s javascript logic behind a login button:

Import-Module wasp
$ie = New-Object -com InternetExplorer.Application
$ie.Navigate("https://taas.citrix.com/AutoSupport/&quot;&#41;
while ($ie.Busy -eq $true){Start-Sleep -Milliseconds 1000}
$ie.Visible=$true

$IEProcess = Select-Window -Title "Citrix Auto Support*" #Retrieves all windows with the specified title

$ie.document.getElementById("username").value = "mycitrixusername"
$ie.document.getElementById("password").value = "supersecretpassword"
$IEProcess | Send-Keys "{Enter}" # Sends Enter button after username and password were entered
while ($ie.Busy -eq $true){Start-Sleep -Milliseconds 1000}
# do what you want after the login process has completed