使用Excel vba登录javascript网页

时间:2015-07-06 03:49:41

标签: javascript excel-vba webforms submit username

我已经浏览了几个论坛,但不幸的是我的html技能不是他们应该做的。有人可以帮我找到使用Excel VBA登录www.emo.no的正确命令吗? (见底部代码)。

我已成功设法使用以下代码填写Google搜索栏:

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

我正在尝试将Excel VBA登录到www.emo.no网站,但我无法找到要填写的正确字段。

我试图让Excel VBA登录的网页上的代码是:

    <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>

我已经尝试过&#34; uid&#34;,&#34; uid1&#34;,&#34; uid2&#34;,&#34; uid_temp&#34;,&#34; div1& #34;,&#34; div2&#34;,&#34; pass_temp&#34;和&#34; pwd&#34;,但屏幕上没有任何操作。

以下是我用于网站的代码:

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

我对这里放什么感到茫然。任何人都可以告诉我填写这两个字段的命令以及提交和登录吗?

1 个答案:

答案 0 :(得分:0)

致@nhee帮助我找到解决方案。

工作代码如下所示:

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