Login to javascript webpage with Excel vba

2019-08-25 08:20发布

问题:

I've looked through several forums, but unfortunately my html skills are not what they should be. Can somebody please help me find the correct command to log onto www.emo.no with Excel VBA? (See code in bottom).

I have successfully managed to fill in the google searchbar with this code:

Sub Google_Test()

Set ie = CreateObject("InternetExplorer.application")

Bnavn = "UserNa"

With ie
    .Visible = True
    .navigate "www.google.com"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

ie.document.all("q").Value = Bnavn


Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


End With
Set ie = Nothing

End Sub

I am trying to have Excel VBA log into the site www.emo.no for me, but am unable to find the correct fields to fill in.

The code on the webpage i am trying to have Excel VBA log onto is:

    <div id="PcLogin">

        <script language="Javascript">
        var locked = false;

      function doLogin() {
        document.login.uid.value = document.login.uid.value.toLowerCase();
        document.login.pwd.value = document.login.pwd.value.toLowerCase();
        return true;
        }


if(!document.all){
  document.captureEvents(Event.CLICK | Event.FOCUS);

}

/* Code for putting the visible word "Username" in the userID field */

 function changeBoxUid()
 {
    document.getElementById('uid1').style.display='none';
    document.getElementById('uid2').style.display='';
    document.getElementById('uid').focus();
 }

 function restoreBoxUid()
 {
    if(document.getElementById('uid').value=='')
    {
      document.getElementById('uid1').style.display='';
      document.getElementById('uid2').style.display='none';
    }
 }
/* End code for userID field */


/* Code for putting the visible word "Password" in the password field */

 function changeBoxPwd()
 {
    document.getElementById('div1').style.display='none';
    document.getElementById('div2').style.display='';
    document.getElementById('pwd').focus();
 }
 function restoreBoxPwd()
 {
    if(document.getElementById('pwd').value=='')
    {
      document.getElementById('div1').style.display='';
      document.getElementById('div2').style.display='none';

    }
 }
/* End code for password field */

</script><div id="loginBody">
<form target="_top" method="post" id="login" name="login" action="ePortal/ctrl" onsubmit="
                if (locked) {
                    return false; 
                } 
                else 
                {
                    locked=true;
                    doLogin();
                };          
            ">
<div class="PcLoginPositionInput">
<input value="user" name="action" type="hidden"><input value="login" name="spec" type="hidden">
<div id="uid1">
<input onfocus="changeBoxUid()" name="uid_temp" type="text" class="PcLoginInputFields" value="Brukernavn">
</div>
<div id="uid2" style="display: none;">
<input onblur="restoreBoxUid()" value="" id="uid" name="uid" type="text" class="PcLoginInputFields">
</div>
<div id="div1">
<input onfocus="changeBoxPwd()" type="text" name="pass_temp" class="PcLoginInputFields" value="Passord">
</div>
<div style="display:none" id="div2">
<input onblur="restoreBoxPwd()" value="" type="password" id="pwd" name="pwd" class="PcLoginInputFields">
</div>
</div>
<div class="PcLoginForgottenPwd">
<a class="login" href="ePortal/ctrl?action=requestnewpassword">Glemt passord?</a>
</div>
<div id="PcLoginRegister"></div>
<input value="" type="submit" class="login_rollover">
</form>
</div>


    </div>

I've tried "uid", "uid1", "uid2", "uid_temp", "div1", "div2", "pass_temp" and "pwd", but there is no action on screen.

Here is the code i am using for the site:

Sub EMO_login()
' Source for this code:
' http://stackoverflow.com/questions/24038230/fill-user-name-and-password-in-a-webpage-using-vba
' http://stackoverflow.com/questions/25319983/logging-in-to-website-with-excel-vba

Set ie = CreateObject("InternetExplorer.application")

Bnavn = User1
Pw = testPass1

With ie
    .Visible = True
    .navigate "www.emo.no"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


'This is where the input command should be.


Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


End With
Set ie = Nothing

End Sub

I am blank as to what to put in here. Can anybody show me the command to fill in the two fields as well as submit and log in?

回答1:

Thans to @nhee for helping me find a solution.

The working code looks like this:

Sub EMO_login()

Dim ie As Object
Dim sht As Worksheet

Set sht = Sheet8
Set ie = CreateObject("InternetExplorer.application")

With ie
    .Visible = True
    .navigate "www.emo.no"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

'Login procedure
ie.document.getElementById("uid2").getElementsByTagName("input")(0).Value = "MyUsername"
ie.document.getElementById("div2").getElementsByTagName("input")(0).Value = "MyPassword"
ie.document.forms("login").submit

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

End With
Set ie = Nothing

End Sub