簡體   English   中英

從Internet Explorer下拉列表中選擇選項

[英]Select option from Internet Explorer dropdown list

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

這是引用下拉列表的HTML代碼:

下拉列表的HTML代碼

這是一個單選按鈕的代碼 :單選按鈕的HTML代碼

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

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. 改成

    htmlSelect.Value =“4 - POSTDEPARTURE”

  2. 請參閱索引值。 即如果它是下拉列表中的第一項,那就是項目(0)。

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

對於單選按鈕,請在引號中包含“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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM