2015-05-13 179 views
0

首先,代碼如下:(該代碼與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與以前一樣工作。

回答

1

嘗試:

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

@TimVilliams嘿添,該.Focus()給出語法錯誤,這只是針對VB.net可能? – Alex

+0

@TimVilliams我用.Focus()= True編輯過,它可以消除語法錯誤,但密碼字段仍然是空的 – Alex

+0

我不能在沒有看到完整頁面源的情況下提出更多建議。 –

1

我不認爲重點有什麼與你的問題。這就像說你不能Range("B1") = "abc"如果A1是ActiveCell。只要您直接將字符串傳送到.Value屬性,重點就不應該成爲問題。如果你正在發送按鍵,那麼或許焦點變得重要,但我們不打算這樣做。

我遇到了一個類似的情況,它是一個多用途的登錄頁面。同一個「登錄頁面」爲買賣雙方提供了單獨的登錄表單。我懷疑仔細檢查該頁面後面的HTML會顯示實際上有兩個具有相同名稱/ ID的元素(例如os_password)。除此之外,.Document.all.Item("something")方法可用於名稱或ID;不是分配值的最權威的方法。看來你正在設置一些標識爲os_password;只是不正​​確os_password

如果出現這種情況,您將不得不將輸入的密碼作爲您想要處理的表單的子項輸入,而不是整個文檔主體。如果您有形式Second_Form的ID,那麼你就可以訪問輸入字段這樣的:

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 
+0

嗨Jeeped謝謝你的回答。我在整個源頁面上進行了搜索,並且這個問題上顯示的4個'os_password'是唯一的匹配項。另外,兩個os_username/os_pasword都採用「loginform」相同的形式。我用你的代碼,它給了我錯誤438 - Obj不支持.Value行上的這個屬性或方法 – Alex

+0

哦,那是我的2¢。在極少數情況下,即使是一個盲人也可以擊中靶心,但這不是其中之一。 – Jeeped

+0

沒有你的方法是絕對正確的 - 在分配前定位正確的組。 – Alex

相關問題