2010-03-03 51 views
2

我正在嘗試編寫vba代碼來填寫web表單併爲我單擊按鈕。我正在瀏覽頁面上的各種option標籤以選擇我想要的標籤。當我達到它時,我想選擇它,但我不確定語法。HTMLInputElement被點擊,HTMLOptionElement獲取

Dim htmlO As HTMLOptionElement 
For Each htmlO In Object.getElementByTagName("option") 
    If Trim(htmlO.value) = "INS" Then 
     htmlO.???? (click? select?) 
     Exit For 
    End If 
Next 

下面是來自網頁的HTML:

<select gtbfieldid="40" id="menu" name="pv_choice"> 
<option selected="selected" value="ZZZ">Choose Menu Option 
</option><option value="BL_COMP">Blanket Companies 
</option><option value="CARR_SEARCH">Carrier Search 
</option><option value="PASSWORD">Change Password 
</option><option value="FED_REG">FMCSA Register 
</option><option value="FEEDBACK">Feedback 
</option><option value="HOME">Home Page 
</option><option value="INS">Insurance Filing 
</option><option value="OOS_LIST">Out Of Service Carriers 
</option></select> 

我要選擇選項 「INS」。

回答

0

到底是什麼工作了......

昏暗htmlO作爲HTMLSelect元
集HTMLS = objdoc.getElementById(「菜單」)
htmlS.selectedIndex = 7

我不得不引用整個菜單並選擇選擇哪一個,不是選擇單個選項。

1

我沒有用VBA所以我提前道歉,但假設它使用了DOM比其他語言我熟悉的相同的結構,嘗試:

htmlO.selected= "selected" 

這是我會怎樣做到這一點在JavaScript :)

HTH

+0

謝謝你的回答...但它不起作用。當我做htmlO.check時,菜單選項沒有被選中。 – dmr 2010-03-03 18:45:37

+0

對不起,我是一個doofus,我以爲你正在使用複選框。嘗試selected =「選中」 更新我的原始回覆。 – Gazillion 2010-03-03 18:53:05

+0

我也嘗試過htmlO.select也沒有工作......謝謝:) – dmr 2010-03-03 18:56:09

2
Dim StrFindText as string 
Dim htmlSelect As HTMLSelectElement 
Dim htmlItemOption As IHTMLOptionElement 

Set htmlSelect = Object.getElementById("menu") 
StrFindText = "INS" 

For i = 0 To htmlSelect.options.length - 1 
    htmlItemOption = htmlSelect.options(i) 
    ' UCase(htmlItemOption.text) = UCase(StrFindText) ' if find text options 
    If UCase(htmlItemOption.value) = UCase(StrFindText) Then 
     htmlItemOption.selected = True 
     Exit For 
    End If 
Next i