Log on to Website using VBA

Rhino999

New Member
Joined
Oct 21, 2018
Messages
4
I’m not sure if I’m posting this to the right place but any advice I can get will be appreciate.
I want to Automate the downloading of files from 2 websites using Excel VBA.


The first website Log on page has name’s and id’s for the Log on elements.
The problem with the first website is that when I load the UserName and the password programmatically then manually click the Logon Button to test it, the UserName is rejected and the page asks to Re-enter the UserName. If I manually enter the UserName, I can Log on with the already loaded password once I click the Log on Button.
Can some tell me why I am getting rejected with programmatically loading the UserName. I noticed that when I manually type the UserName an 'X' appears at the end of the Box.


Here is a snippet of the code.
<input name="p$lt$ctl02$LogonShawForm$Login1$UserName" type="text" maxlength="100" id="p_lt_ctl02_LogonShawForm_Login1_UserName" class="simple form-control WatermarkText" autocomplete="off">





The second problem with the other web site is that the ‘SIGN IN’ button does not have any name or id that I can find.
I know it is in a Table but I do not know what name, class or tag name to reference.


This is the element that I’m trying to Click.
<input type="submit" value="SIGN IN" style="margin:5px 0px 5px 50px;background-color:#000000;color:#FFFFFF;
padding:3px 10px 3px 10px;border:2px solid black;font-weight:bold;" *******="update();return false;">




Below is some of the source code.

HTML:
******** language="JavaScript" type="text/javascript">
 function update() {
  if (validInput()) {
    self.status="Login is in progress. Please wait...";
    document.frmLogin.txtUserID.value = document.frmLogin.txtUserID.value.toUpperCase();
    document.frmLogin.txtPassword.value = document.frmLogin.txtPassword.value.toUpperCase();
    document.frmLogin.submit();
  }
}
****** class="body" topmargin="5" ******="defaultStatus='Please Login to MohawkNet.';onPageLoadFocus();" 
  style="background-color: black;margin-bottom:0px;margin-top:10px">
<!--******** type="text/javascript" src="<!%= request.getContextPath() %>/javascript/ajax.js">*********>-->
<!--******** type="text/javascript" src="<!%= request.getContextPath() %>/javascript/floating-window.js">*********>-->
******** language="javascript" type="text/javascript">
  <!-- break out of frames
    if (window != top) top.location.href = location.href;
  // -->
  *********>
  
<div class="OuterPanel" align="center">
  <div style="background-color: #000;height:10px; margin:0 0 0 -580px;"></div>
<div id="display" >
<input type="hidden" id="urlprefixhidden" value="/mnet">
<table width="960px" class="newfeatures" cellspacing="0" cellpadding="0" border="0">
	<tr style="background-color: white;">
		<td valign="top" style="background: #E8E0C9 url('/mnet/images/V1Fullhomescreen-Mohawknet.com.png');
           height:454px;width:960px;background-repeat: no-repeat;">
         <div style="margin:0 0 0 40px;">
  		   <img src="/mnet/images/Logo_Login.png" alt="" />
         </div>
		 <div style="width:320px;margin:18px 0px 0px 600px;">
		   <div id="TableTopRedlogin" name="TableTopRedlogin"> </div>
		 <form method="post" name="frmLogin"	action="/mnet/servlet/MACSAppMgr">
		     <table style="background-color:#EE0F00;border-spacing:0px;"width="100%">
		     <tr>
			   <td colspan="3">
				 <div id="errorDiv" class="ErrorPanel" style="display: none;top:38%;background:#C7C0C1;">
				   <table width="100%">
					 <tr><td id="errormsg" style="background:#000000;color:#FFFFFF;">ATTENTION!</td></tr>
					</table>
					<table cellpadding="2" width="100%">
					   <tr>
						 <td align="center" valign="middle" width="25%"><img width="32" height="32" src="/mnet/images/important.png" alt=""></td>
						 <td><span id="errorSpan" class="errorMsg"></span></td>
						</tr>
						<tr>
						 <td align="center" valign="middle" colspan="2"><input type="BUTTON" value="OK" class="button"	*******="document.getElementById('errorDiv').style.display='none'; focusError();"></td>
						</tr>
					</table>
					</div>
				</td>
			</tr>
		      <tr>
		       <td width="30px"> </td>
		       <td  class="loginSectionText" style="padding-left:15px;">
		         <table>
		          <tr><td colspan="2" style="font-size:22px;color:#FFFFFF">GLAD YOU'RE BACK</td></tr>
		          <tr>
		            <td colspan="2">
		             <div style="background:#ABACAC;height:1px;"></div>
		            </td>
		          </tr>
		          <tr>
		            <td colspan="2" style="font-size:18px;color:#000000">
		              <input type="hidden" name="RequestName" value="Logon"> 
		              <input type="hidden" name="AttemptNumber"	value="1">
		              LOG IN 
		             </td>
		            </tr>
		           <tr>
		              <td style="padding:10px 0px;font-size:17px;color:#FFFFFF">User ID</td>
		              <td ><input type="text" name="txtUserID" size="15" maxlength="10"></td> 
		           </tr>
		           <tr>
		              <td style="font-size:17px;color:#FFFFFF">Password</td>
		              <td ><input type="password" name="txtPassword" size="15" maxlength="10"></td> 
		           </tr>
		            <tr style="text-align:center;">
		              <td colspan="2">
		              <input type="submit" value="SIGN IN" 
		                 style="margin:5px 0px 5px 50px;background-color:#000000;color:#FFFFFF;
		                 padding:3px 10px 3px 10px;border:2px solid black;font-weight:bold;"*******="update();return false;">
Thank you in advance for your help.
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The first website Log on page has name’s and id’s for the Log on elements.
The problem with the first website is that when I load the UserName and the password programmatically then manually click the Logon Button to test it, the UserName is rejected and the page asks to Re-enter the UserName. If I manually enter the UserName, I can Log on with the already loaded password once I click the Log on Button.
Can some tell me why I am getting rejected with programmatically loading the UserName. I noticed that when I manually type the UserName an 'X' appears at the end of the Box.


Here is a snippet of the code.
<input name="p$lt$ctl02$LogonShawForm$Login1$UserName" type="text" maxlength="100" id="p_lt_ctl02_LogonShawForm_Login1_UserName" class="simple form-control WatermarkText" autocomplete="off">
Does this element have any events? Use your browser's developer tools (press the F12 key) to find out. You also need to check parent elements for events.

Some websites typically have a keyup/keydown/keypress event and a change event on text input elements, so as a guess if yours has a keyup and change event the code might be something like this (add a reference to Microsoft HTML Object Library, via Tools->References in the VBA editor):

Code:
    Dim HTMLdoc As HTMLDocument
    Dim usernameInput As HTMLInputElement
    #If VBA7 Then
        Dim keyupEvent As DOMKeyboardEvent
        Dim changeEvent As DOMMouseEvent
    #Else
        Dim keyupEvent As Object
        Dim changeEvent As Object
    #End If

    'After the page has completely loaded in IE
    
    Set HTMLdoc = IE.document

    Set keyupEvent = HTMLdoc.createEvent("HTMLEvents")
    keyupEvent.initEvent "keyup", True, False
    Set changeEvent = HTMLdoc.createEvent("MouseEvents")
    changeEvent.initEvent "change", True, False

    Set usernameInput = HTMLdoc.getElementById("p_lt_ctl02_LogonShawForm_Login1_UserName")
    usernameInput.Focus
    usernameInput.Value = "Your username"
    usernameInput.dispatchEvent keyupEvent
    DoEvents
    usernameInput.dispatchEvent changeEvent
    DoEvents



The second problem with the other web site is that the ‘SIGN IN’ button does not have any name or id that I can find.
I know it is in a Table but I do not know what name, class or tag name to reference.


This is the element that I’m trying to Click.
<input type="submit" value="SIGN IN" style="margin:5px 0px 5px 50px;background-color:#000000;color:#FFFFFF;
padding:3px 10px 3px 10px;border:2px solid black;font-weight:bold;" *******="update();return false;">
2 ways: get a collection of input elements (tags) and loop through them looking for the "SIGN IN" one and click that, or more simply, since that input element has type="submit", submit the parent form, assuming it is the first (0) form on the page:

Code:
    IE.document.forms(0).submit
I'm guessing that ******* is o_nclick, without the underscore. Therefore you may need to fire the click event, in a similar way to the first website.
 
Upvote 0
John_w,
thanks so much for responding.

With the code and insight you provided I was able to Log into both websites.

Now that I can Log on I can move forward with trying to set all the parameters to download the files I'm interested in.

Again, thanks so much for your help, without it, I would still be stuck.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top