【问题标题】:Select option from Internet Explorer dropdown list从 Internet Explorer 下拉列表中选择选项
【发布时间】:2017-02-10 18:11:55
【问题描述】:

我正在打开一个网页并填写一些字段。我设法填充了文本框,但在从下拉列表检查/选择单选按钮中选择选项时遇到问题。

这是引用下拉列表的 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

【问题讨论】:

    标签: html vba excel internet-explorer


    【解决方案1】:

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

    1. 改成

      htmlSelect.Value = "4 - 出发后"

    2. 参考索引值。 IE。如果它是下拉列表中的第一项,则为 item(0)。

      htmlSelect.item(0).selected="True"

    对于您的单选按钮,将“true”括在引号中。此外,您还需要遍历具有该特定名称的所有元素(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
    

    【讨论】:

    • 谢谢 RyanL,它现在正在工作!我对循环没有太多经验,但我设法让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多