2015-11-13 92 views
0

我正在使用vba自動化Internet Explorer中的站點。在這一點上,我一直非常成功。我遇到的問題是有一個輸入字段,類型是「隱藏」,而不是文本。我如何解決這個問題?我知道必須有一種方式。使用VBA和輸入類型「隱藏」的IE自動化(IE8)

感謝您的任何建議。

+0

我猜你正在處理網頁表單,那麼你可以分享表單的HTML內容嗎?還有你的VBA代碼? – omegastripes

+0

如果您顯示您嘗試的代碼並更準確地解釋您收到了哪些錯誤或哪些工作不正常,那更好。通常,您將參考設置爲隱藏表單字段,您可以像設置常規文本輸入字段一樣設置其值。 –

回答

1

下面的例子演示瞭如何通過指數環是指輸入元素:

Sub Test() 
    Set objIE = CreateObject("InternetExplorer.Application") 
    objIE.Visible = True 
    ' navigate and download the web page 
    objIE.Navigate "https://www.yahoo.com/" 
    Do While objIE.ReadyState <> 4 Or objIE.Busy 
     DoEvents 
    Loop 
    ' retrieve document object 
    Set objDocument = objIE.document 
    ' retrieve forms collection 
    Set colForms = objDocument.forms 
    ' retrieve the first form object by index 
    Set objForm = colForms(0) 
    ' retrieve the form input tags collection 
    Set colInputTags = objForm.getElementsByTagName("input") 
    ' loop through all input tags in the form 
    For n = 0 To colInputTags.Length - 1 
     ' refer to the certain input tag by index 
     Set objInputTag = colInputTags(n) 
     ' output 
     Debug.Print n _ 
      & " (" & objInputTag.Type & ") " _ 
      & objInputTag.Name & " = " _ 
      & objInputTag.Value 
    Next 
    ' refer to input tag #0 
    Set objElement = colInputTags(0) 
    ' hide the search inputbox 
    objElement.Type = "hidden" 
    ' refer to input tag #4 
    Set objElement = colInputTags(4) 
    ' output the current value 
    Debug.Print "#4 value = " & objElement.Value 
    ' cnange the value 
    objElement.Value = "input tag #4" 
    ' cnange the type 
    objElement.Type = "Text" 
    ' refer to input tag #5 
    Set objElement = colInputTags(5) 
    ' cnange the value 
    objElement.Value = "input tag #5" 
    ' cnange the type 
    objElement.Type = "Text" 
    ' quit IE 
    objIE.Quit 
End Sub 

的代碼給我下面的輸出:

immediate window

正如你所看到的,隱藏主輸入標籤,輸出隱藏的輸入標籤#4的值,並且改變#4和#5的類型和值,因此網頁看起來像:

modified web page

+0

比你非常感謝你的幫助。我仍然對如何將文本元素放入類型爲「隱藏」的輸入框感到困惑,我似乎無法獲得改變的價值。 – hockeybo97

+0

@ hockeybo97,我添加了描述如何更改隱藏的輸入內容。 – omegastripes

+0

非常感謝。我已經獲得了成功改變的價值。我現在面臨的挑戰是如何將它放到窗體上的輸入框中。這裏是一個源代碼的例子我想讓Input Name等於我指定的值。 – hockeybo97