使用Excel vba登录到javascript网页 [英] Login to javascript webpage with Excel vba

查看:71
本文介绍了使用Excel vba登录到javascript网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经成功地用以下代码填充了Google搜索栏:

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

我正在尝试让Excel VBA为我登录网站www.emo.no,但是无法找到正确的字段来填写.

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.

我要登录Excel VBA的网页上的代码是:

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>

我尝试了"uid","uid1","uid2","uid_temp","div1","div2","pass_temp"和"pwd",但屏幕上没有任何操作.

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?

推荐答案

感谢@nhee帮助我找到解决方案.

Thans to @nhee for helping me find a solution.

工作代码如下:

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

这篇关于使用Excel vba登录到javascript网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆