我在HTA中有一个VBS脚本,它应该通过尝试使用相同凭据登录外部网页来验证HTA登录表单的登录凭据。 " getElementsByClassName方法"似乎没有工作,因为每次我运行它输出的脚本:
对象不支持此属性或方法:' IE.Document.getElementsByClassName(...)。innerHTML'
此脚本应该在IE中打开登录页面,输入登录凭据并测试错误消息。如果凭据错误,页面上会显示错误消息,并且类别为#34; alert alert-error"。
<script language="vbscript">
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.navigate "https://www.mypage.com/login"
Do Until IE.ReadyState = 4
wscript.sleep 200
Loop
IE.Document.All.Item("username").Value = "loginUsername"
IE.Document.All.Item("password").Value = "loginPassword"
IE.Document.All.Item("submit").Click
Do Until IE.ReadyState = 4
wscript.sleep 200
Loop
set error = IE.Document.getElementsByClassName("alert alert-error").innerHTML
if error="" then
X=MsgBox("You have successfully been logged in!")
else
X=MsgBox("An error occured, you are not logged in.")
end if
</script>
&#13;
我希望这是解决问题的足够信息。谢谢!
答案 0 :(得分:1)
首先,getElementsByClassName()
返回指定className的元素对象的集合,它不是单个对象。因此语句getElementsByClassName("alert alert-error").innerHTML
无效并将生成错误。
您必须枚举集合,以类似于以下方式定位其中的对象:
var colAlertErrorElements = IE.Document.getElementsByClassName("alert alert-error");
for (var i=0; i<colAlertErrorElements.length; i++) {
var objErrorElement = colAlertErrorElements[i];
if (objErrorElement.innerHTML == "') {
// no error
}
else {
// error
}
}
答案 1 :(得分:0)
您必须编写自己的fnc,它不会嵌入DOM对象中。我基本上是翻译了几年前写的一个旧的JavaScript fnc,用于通过Web进行管理员管理。 DOM提供与查询语言无关的相同功能(js,vbs等)。
我包括一个'GetParentTagName'fnc来跳过从正文文档中搜索(更快)。有关核心脚本,请参考'GetElementsByClass'fnc。您可以使用更通用的名称,即搜索任何属性名称,而不仅是类属性。
以下是代码(用于HTA的HTML ex + VBS):
HTML code
=========
<table name="product_not_approve">
<tbody name="cieA" >
<tr class="tr_205F51AF-4245-4258-96A3-76DC13B978BB">
<td fieldname="checkbox_delete">
<input type="checkbox" name="delete_opt_cieA" value="205F51AF-4245-4258-96A3-76DC13B978BB" />
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>
<a href="#" onclick="Admin_Accept_Save_Change me">Make change</a>
</td>
</tr>
</tfoot>
</table>
VBS code
' =================================================================
Sub Admin_Accept_Save_Change(this)
'msgbox this = window.event.srcElement
'msgbox this.outerHTML
Dim oEleParent : Set oEleParent = GetParentTagName(this, "table")
'msgbox oEleParent.tagName & " - " & oEleParent.outerHTML
' Fnc GetElementsByClass((searchClass, node, tag)) return a dictionary object, node can be null, search from DOM body
Dim oEles : Set oEles = GetElementsByClass("tr_" & "205F51AF-4245-4258-96A3-76DC13B978BB", oEleParent, "tr")
Dim oEle
For i = 0 To oEles.Count - 1
'msgbox oEles.Keys()(i) & ", " & TypeName(oEles.Items()(i))
Set oEle = oEles.Items()(i)
msgbox oEle.outerHTML
Next
' Cancel href event
window.event.returnValue = False
End Sub
' =================================================================
Function GetParentTagName(oEle, tagName)
Dim oParent : Set oParent = oEle.parentNode ' oEle.parentElement.outerHTML
Do While UCase(oParent.tagName) <> UCase(tagName)
Set oParent = oParent.parentNode
If UCase(oParent.tagName) = "BODY" Then
Set oParent = Nothing
Exit Do
End If
Loop
Set GetParentTagName = oParent
End Function
' =================================================================
Function GetElementsByClass(searchClass, node, tag)
If IsNull(node) Then Set node = document
If IsNull(tag) Or tag = "" Then tag = "*"
Dim oElements : Set oElements = node.getElementsByTagName(tag)
Dim oDic : Set oDic = CreateObject("Scripting.Dictionary")
oDic.CompareMode = vbTextCompare
' msgbox "typename node = " & TypeName(node) & vbCrLf & _
' "tag = " & tag & vbCrLf & _
' "searchClass = " & searchClass & vbCrLf & _
' "nb of ele found = " & oElements.length
Dim i : i = 0
Dim oEle, oAtt
Do While i < oElements.length
Set oEle = oElements(i)
Set oAtt = oEle.Attributes.getNamedItem("class") ' Return a DispHTMLDOMAttribute object
If oAtt.value = searchClass Then
'msgbox "Found class : " & oAtt.name & " - " & oAtt.value
oDic.Add oDic.count + 1, oEle
End If
i = i + 1
Loop
'msgbox "Nb of DOM element found : " & oDic.Count
Set GetElementsByClass = oDic
End Function