[英]I Get "object required" error when using getElementByID
I am trying to enter a Username into a text box Using getElementById.我正在尝试使用 getElementById 在文本框中输入用户名。 this generates an "Object required" Error
这会生成“需要对象”错误
Dim Myid
Mypsw As String
Dim ie As InternetExplorer
Dim ErrCnt
On Error GoTo ErrorHandler
ChDir "C:\Users\hm_cl\Downloads"
Kill "History.txt"
Myid = "hmclark1959"
Mypsw = "xxxxxxxxx"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://eteller.dakotalandfcu.com/Centryx/servlet/com.sos.webteller.accountaccess.LoginFrame"
While ie.Busy = True Or ie.readyState < 4: DoEvents: Wend
' ******** The following line generates the error
ie.document.getElementById("LoginID").Value = Myid
ErrorHandler:
MsgBox (Err.Description)
Select Case Err.Number
Case 53 ' File not found
Resume Next
Case 424 ' Object Reuired
ErrCnt = ErrCnt + 1
If ErrCnt > 4 Then
MsgBox (" Unknown Server Side Error")
ie.Quit
End
Else
Application.Wait (Now + TimeValue("0:00:10"))
Resume
End If
Case Else 'Unknown error
MsgBox ("There is an unhandled error")
ie.Quit
End
End Select
When I click inspect element on the textbox it says the ID="LoginID"当我单击文本框上的检查元素时,它显示 ID="LoginID"
The page still runs in IE.该页面仍在 IE 中运行。 But I give to consider, IE is actively being phased out by MS.
但我考虑一下,IE 正在被 MS 积极淘汰。
This is the html part in question这是有问题的 html 部分
<p id="LoginId"> <label>Login Id</label> <input type="text" name="LoginId" maxlength="32" value="" autocomplete="off" tabindex="0"> <span class="checkdigit"></span> </p>
As you see LoginID
is the id of the p tag but you need the input tag to enter your text.如您所见,
LoginID
是 p 标签的 ID,但您需要输入标签才能输入文本。 The input tag is the first child and than the first sibling in the dom tree from the p tag.输入标签是第一个孩子,而不是 p 标签的 dom 树中的第一个兄弟姐妹。 Try this:
尝试这个:
ie.document.getElementById("LoginID").firstChild.nextSibling.Value = Myid
Look here for further information:在这里查看更多信息:
firstChild第一个孩子
nextSibling下一个兄弟姐妹
Something about the Document Object Model (DOM)关于文件 Object Model (DOM)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.