VBA document.all.item只适用于用户名而不是密码

首先,代码如下:(代码与vba-open-website的标准代码没有区别,如果这样可以节省您的时间阅读,我就放置了一个<——— HERE来指明我的密码)

Sub Login_2Wiki() Dim ieApp As InternetExplorer Dim ieDoc As Object Dim ieTable As Object Dim clip As DataObject Dim iusr As Object Dim ipwd As Object Dim iform As Object 'create a new instance of ie Set ieApp = New InternetExplorer 'you don't need this, but it's good for debugging ieApp.Visible = True 'assume we're not logged in and just go directly to the login page ieApp.Navigate "https://xxx.xxxxx.com/login.action?" Do While ieApp.Busy: DoEvents: Loop Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop Set ieDoc = ieApp 'fill in the login form – View Source from your browser to get the control names With ieDoc Set iform = .Document.getElementById("login-container") Set iusr = iform.Document.getElementById("os_username") iusr.Value = "guest" Set ipwd = iform.Document.getElementById("os_password") ipwd.Value = "abc" .Document.getelementsbyname("loginButton")(0).Click End With Do While ieApp.Busy: DoEvents: Loop Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 'close 'er up ieApp.Quit Set ieApp = Nothing End Sub 

这里是网页上的源代码的一部分(如果这是重要的Atlassian Confluence页面):

 <div class="field-group " > <label id="os_username-label" for="os_username"> Username </label> <input type="text" name="os_username" id="os_username" class="text " placeholder="Username" data-focus="0" /> </div> <div class="field-group"> <label id="os_password-label" for="os_password"> Password </label> <input type="password" name="os_password" id="os_password" class="password " placeholder="Password" /> </div> 

问题:代码只填写了用户名,而没有在网页上填写密码。 它没有任何错误。 另外我注意到,在网页的源视图中有一个data-focus =“0” ,并想知道是否是造成问题的原因。

编辑:我用.Document.getElementById方法replace.Document.all.Item ,虽然代码可以find元素os_password(没有错误),它无法为其分配值。 os_username和以前一样工作。

尝试:

 With .Document.getElementById("os_password") .Focus() .Value = "abc" End With 

我不相信你的问题关注焦点。 这就像是说如果A1是ActiveCell ,你不能Range("B1") = "abc" 。 只要您直接将string传递给.Value属性,焦点不应该成为问题。 如果你正在发送击键,那么或许重点变得重要,但我们不打算这样做。

我遇到了类似的情况,这是一个多用途的login页面。 同一个“login页面”有买卖双方的单独login表单。 我怀疑仔细检查该页面后面的HTML会显示实际上有两个具有相同名称/ ID的元素(例如os_password )。 除此之外, .Document.all.Item("something")方法可用于名称或ID; 而不是分配数值的最权威的方法。 看起来你正在设置一些标识为os_password的东西; 只是不正​​确的os_password

如果出现这种情况,则必须将input的密码作为您要处理的表单的子项,而不是整个文档主体。 如果您的表单具有Second_Form的ID,那么您可以像这样访问input字段:

  dim eFRM as MSHTML.IHTMLElement set eFRM = ieApp.document.getElementById("Second_Form") eFRM.getElementById("os_username").Value = "guest" eFRM.getElementById("os_password").Value = "abc" 'don't use names to locate elements - horribly unreliable eFRM.submit