从Internet Explorer下拉列表中选择选项

时间:2017-02-10 18:11:56

标签: html excel vba excel-vba 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

1 个答案:

答案 0 :(得分:0)

关于您的列表/下拉列表,我认为有两种可能的解决方案。

  1. 更改为

    htmlSelect.Value =“4 - POSTDEPARTURE”

  2. 请参阅索引值。即如果它是下拉列表中的第一项,那就是项目(0)。

    htmlSelect.item(0).selected = “真”

  3. 对于您的单选按钮,请在引号中包含“true”。此外,您需要遍历具有该特定名称的所有元素(getElementbyID仅返回1个项目,但getElementsByName可以引用多个元素)。

    e.g。

    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