VBA - 如何输入用户名,密码并单击网页中的提交按钮

时间:2016-11-16 07:30:42

标签: excel-vba vba excel

您好我是VBA的新手,我正在尝试使用VBA自动登录网页,但我在少数网站上取得了成功,但对于这个特定的网页,我无法做到这一点

以下是我的VBA代码:

Sub login ()
    Set IE = CreateObject("InternetExplorer.application")
    IE.AddressBar = 0
    IE.StatusBar = 0
    IE.Toolbar = 0
    IE.Visible = True
    IE.navigate "www.sample.com"
    Do Until Not IE.busy: DoEvents: Loop
    Set doc = IE.document
    Do While doc.ReadyState <> "complete": DoEvents: Loop
    IE.document.all("ocsid").Value = "xx"
    IE.document.all("password").Value = "yy"
    IE.document.all("Submit").Click
End Sub

以下是我从网页上复制的登录区域的HTML代码

    </TR>
    <TR>
    <TD width="165"><FONT FACE="Arial, Helvetica" SIZE=2><b>OCS Id:</b></FONT></TD>
    <TD align="center" width="95"><INPUT size="15" type="text" maxlength="10" name="ocsid"></TD>
     </TR>
     <TR>
     <TD width="165"><FONT FACE="Arial, Helvetica" SIZE=2><b>OCS Password</b></FONT>:</TD>
     <TD align="center" width="95"><INPUT size="15" type="password" maxlength="8" name="password"></TD>
     </TR>
     <TR>
     <TD width="165"><FONT FACE="Arial, Helvetica" SIZE=2><b>X500 Id:</b></FONT></TD>
     <TD align="center" width="95"><INPUT size="15" type="text" maxlength="10" name="x500id"></TD>
     </TR>

    <TR>
    <TD height="15" width="165"></TD>
    <TD width="95"></TD>
    </TR>
    <TR>
    <TD align="center" width="165" colspan="2"><INPUT type="checkbox" name="changePasswordBox"  onclick="changePassword2();" />
    <FONT FACE="Arial, Helvetica" SIZE=2><b>Change Password</b></FONT></TD>
    </TR>                    
    </TABLE>
    <div id="HideArea" >
    <TABLE width="287">
    <TR>
    <TD width="165"><FONT FACE="Arial, Helvetica" SIZE=2><b>New Password:</b></FONT></TD>
    <TD align="center" width="95"><INPUT size="15" type="password" maxlength="8" name="newpassword"></TD>
    </TR>

<TR>
<TD width="165"><FONT FACE="Arial, Helvetica" SIZE=2><b>Verify New Password:</b></FONT></TD>
<TD align="center" width="95"><INPUT size="15" type="password" maxlength="8" name="VERIFY_NEW_PWD" ></TD>
</TR>
<TR>
</TABLE>
</div>    
<TABLE width="287">
<TR>
<TD height="15" width="165"></TD>
<TD width="95"></TD>
</TR>
<TR>
<TD colspan="2" align="center"><input type="submit" name="Submit" value="Submit" onclick="if (top.frames[0] && top.frames[0].animate) top.frames[0].animate.start();" style="width: 60px">&nbsp;&nbsp;&nbsp;
<INPUT style="width: 60px" type="button" name="Reset" value="Reset" onClick="resetPage();"></TD>
</TR>

我希望这能帮助你回答我的问题!!

评论: -  我很抱歉 !!我不明白你的要求是什么?但我发现了一些东西,当我使用chrome右键单击页面时,它提供了一个额外的选项作为“View Frame Source”,通常我成功自动化上述过程的所有其他网站将只有一个选项作为“查看页面”来源“使用哪个我会找到”ID“和”标签“等等......当我点击”查看页面源“选项时,我没有看到任何”ID“和”标签“而是我只有链接以.jsp格式结束,当我点击链接时,它将我带到网页的一部分(如登录区域)(法律声明和隐私政策)....我不确定这是否会对您有所帮助!但我真的很抱歉,我非常感谢你的工作!!!!

1 个答案:

答案 0 :(得分:1)

不确定是否可以将此作为答案发布,但如果使用getElementsByName按名称选择输入字段呢?

Sub login()
    Dim IE As Object
    Dim doc As Object
    Set IE = CreateObject("InternetExplorer.application")
    IE.AddressBar = 0
    IE.StatusBar = 0
    IE.Toolbar = 0
    IE.Visible = True
    IE.navigate "www.sample.com"
    Do Until Not IE.busy: DoEvents: Loop
    Set doc = IE.document
    Do While doc.ReadyState <> "complete": DoEvents: Loop
    doc.GetElementsByName("ocsid")(0).Value = "xx"
    doc.GetElementsByName("password")(0).Value = "xx"
    doc.GetElementsByName("Submit")(0).Click
End Sub

使用此html文件进行测试:

<input size="15" type="text" maxlength="10" name="ocsid">
<input size="15" type="password" maxlength="8" name="password">
<input type="submit" name="Submit" value="Submit" onclick="alert('it works')">