我不能为我的生活找出如何用VBA点击下面的按钮。任何帮助,将不胜感激。我已经能够填充用户名/密码字段,因为它们有一个名称所以我使用了getElementsByName,但按钮没有名称或ID。
具体的按钮代码是:
<table class="button"><tr><td><div class="button-left"><input type="submit" class="form-button" value="Submit" >
如果有帮助,这是完整的脚本/表单代码
<form name="loginFormBean" method="post" action="/XXXXXXXX/login.do" onsubmit="return validateForm(this)">
<TABLE border="0" cellpadding="5" cellspacing="0">
<TR>
<TD class="bigGreyContent" nowrap>User ID</TD>
<TD class="bigGreyContent" align="left">
<input type="text" name="username" maxlength="50" size="40" value="" class="bgGreenColor">
</TD>
</TR>
<TR>
<TD class="bigGreyContent" nowrap>Password</TD>
<TD class="bigGreyContent" align="left">
<input type="password" name="password" maxlength="50" size="40" value="" class="bgGreenColor">
</TD>
</TR>
<TR>
<TD colspan="2" align="center">
<BR>
<table class="button"><tr><td><div class="button-left"><input type="submit" class="form-button" value="Submit" ></div><div class="button-right"></div></td></tr></table>
</TD>
</TR>
</TABLE>
</form>
编辑:我启用了以下参考(除了默认参考):
Microsoft HTML对象库 Microsoft Internet Controls Microsoft WinHTTP服务,版本5.1 Microsoft XML,v6.0
我目前的代码是:
Sub XXXX()
Dim objIE As InternetExplorer
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim btn As Object
Set objIE = New InternetExplorer
Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0)
objIE.Visible = True
objIE.navigate "http://XXXX/login.jsp"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementsByName("username")(0).Value = "XXXX"
objIE.document.getElementsByName("password")(0).Value = "YYYY"
btn.Click
End Sub
答案 0 :(得分:3)
假设class =“button-left”是你的html中第一次出现,试试这个:
Dim btn As Object
Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0)
btn.Click
修改强>
如果您尚未Microsoft HTML Object Library
,则应添加Microsoft XML v6.0
和Tools -> References
。
目前,您的方法和方法之间存在差异。你需要选择一个,所以请尝试这个:
Sub XXXX()
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim btn, usr, psw As Object
With CreateObject("MSXML2.serverXMLHTTP")
.Open "GET", "http://XXXX/login.jsp", False
.send
html.body.innerHTML = .responseText
End With
Set usr = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(0)
Set psw = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(1)
Set btn = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(2)
usr.Value = "Username"
psw.Value = "Password"
btn.click
Set html = Nothing: Set btn = Nothing: Set usr = Nothing: Set psw = Nothing
End Sub
答案 1 :(得分:0)
您的代码几乎是corect。您需要设置Set html = objIE.document
,然后更改Set btn = html.getElementsByClassName(...)
的位置,使其在文档加载后。顺便说一句。 @Tehscript的答案也适用。
Dim objIE As InternetExplorer
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim btn As Object
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate Path
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set html = objIE.document ' <-- This is the cause of the error 91
Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0) ' <-- Button can be set after the document has been loaded
objIE.document.getElementsByName("username")(0).Value = "XXXX"
objIE.document.getElementsByName("password")(0).Value = "YYYY"
btn.Click