2017-02-10 61 views
1

我正在打開一個網頁並填寫一些字段。我設法填充文本框,但是我無法從下拉列表中選擇檢查/選擇單選按鈕的選項。從Internet Explorer下拉列表中選擇選項

這是HTML代碼指的下拉列表:

HTML code for drop down list

這一個是對一個單選按鈕的代碼: HTML code for radio button

這是我到目前爲止的代碼:

Sub w() 
' 
' w Macro 
' 
' Keyboard Shortcut: Ctrl+w 
' 
'pick ups cell b2 value 
Dim cellvalue As String 
cellvalue = ActiveSheet.Cells(1, 2) 

Dim HTMLDoc As HTMLDocument 
Dim oBrowser As InternetExplorer 
''Sub Login_2_Website() 

Dim oHTML_Element As IHTMLElement 
Dim sURL As String 

On Error GoTo Err_Clear 
sURL = cellvalue ' 
Set oBrowser = New InternetExplorer 
oBrowser.Silent = True 
oBrowser.timeout = 60 
oBrowser.navigate sURL 
oBrowser.Visible = True 

Do 
' Wait till the Browser is loaded 
Loop Until oBrowser.readyState = READYSTATE_COMPLETE 

Set HTMLDoc = oBrowser.document 

'fill email response address 
HTMLDoc.all.emailAddresses.Value = ActiveSheet.Cells(5, 3) 

'fill shipment reference number 
HTMLDoc.all.filingRefNumber.Value = ActiveSheet.Cells(5, 7) 

'fill dropbox option 
'NOT WORKING  
If Not VBA.IsNull(ie.document.getElementById("select2-drop-mask")) Then 
    Dim htmlSelect 
    Set htmlSelect = ie.document.getElementById("select2-drop-mask") 
    htmlSelect.Value = 4 - POSTDEPARTURE 
Else 
    MsgBox "Element 'select2-drop-mask' was not found", vbExclamation 
End If 

'SELECT RADIO BUTTON 
' NOT WORKING 
ie.document.getElementsByName("shipmentInfo.routedExpTransactionInd.stringFiEld").Item(1).Checked = True 

For Each oHTML_Element In HTMLDoc.getElementsByTagName("Login") 
If oHTML_Element.Type = "Login" Then oHTML_Element.Click: Exit For 
Next 

' oBrowser.Refresh ' Refresh If Needed 
Err_Clear: 
If Err <> 0 Then 
'Debug.Assert Err = 0 
Err.Clear 
Resume Next 
End If 
End Sub 

回答

0

關於你的名單/下拉,我認爲有兩種可能的解決方案。

  1. 更改爲

    htmlSelect.Value = 「4 - POSTDEPARTURE」

  2. 參考索引值。即如果它是下拉菜單中的第一項,則爲項目(0)。

    htmlSelect.item(0).selected = 「真」

爲了您的單選按鈕,用引號括 '真'。此外,您需要遍歷所有具有該特定名稱的元素(getElementbyID只返回1個項目,但getElementsByName可以指向多個元素)。

例如

dim objects 
dim obj 

set objects=document.getElementsByName("shipmentInfo.routedExpTransactionInd.stringFiEld") 

for each obj in objects 
obj.item(0).checked=True 
next 

或嘗試:

Set ieRadio = IE.Document.all 
    ieRadio.Item("shipmentInfo.routedExpTransactionInd.stringFiEld")(1).Checked = True 
+0

謝謝RyanL,現在是工作!我沒有太多的循環經驗,但我設法讓它工作。 –

相關問題