首先,代码如下:(该代码与vba-open-website的标准代码没有区别,如果这样可以节省您的时间阅读时间,我会放置一个< - --------这里来指示我把我的密码)VBA document.all.item仅适用于用户名,但不适用于密码
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的合流页面,如果该事项):
<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.all.Item与 .Document.getElementById方法,althought代码可以找到元素os_password(没有错误),也无法分配值到它。 os_username与以前一样工作。
@TimVilliams嘿添,该.Focus()给出语法错误,这只是针对VB.net可能? – Alex
@TimVilliams我用.Focus()= True编辑过,它可以消除语法错误,但密码字段仍然是空的 – Alex
我不能在没有看到完整页面源的情况下提出更多建议。 –