首先,代碼如下:(該代碼與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
我不能在沒有看到完整頁面源的情況下提出更多建議。 –