简体   繁体   English

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

[英]Select option from Internet Explorer dropdown list

I am working on opening a webpage and filling out some fields. 我正在打开一个网页并填写一些字段。 I managed to fill textboxes, but I am having trouble selecting options from drop down lists checking/selecting radio buttons. 我设法填充文本框,但我无法从下拉列表中选择选项来检查/选择单选按钮。

This is the HTML code referring to the dropdown list: 这是引用下拉列表的HTML代码:

HTML code for drop down list 下拉列表的HTML代码

This one is the code for one of the radio buttons: HTML code for radio button 这是一个单选按钮的代码 :单选按钮的HTML代码

This is my code so far: 到目前为止这是我的代码:

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

With respect to your list/dropdown, I think there are two potential solutions. 关于您的列表/下拉列表,我认为有两种可能的解决方案。

  1. Change to 改成

    htmlSelect.Value = "4 - POSTDEPARTURE" htmlSelect.Value =“4 - POSTDEPARTURE”

  2. Refer to the index value. 请参阅索引值。 Ie if it's the first item in the dropdown, it's item(0). 即如果它是下拉列表中的第一项,那就是项目(0)。

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

For your radio button, wrap 'true' in quotes. 对于单选按钮,请在引号中包含“true”。 Also you'd need to loop through all elements with that particular name (getElementbyID only returns 1 item, but getElementsByName can refer to multiple elements). 此外,您需要遍历具有该特定名称的所有元素(getElementbyID仅返回1个项目,但getElementsByName可以引用多个元素)。

eg 例如

dim objects
dim obj

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

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

or try: 或尝试:

 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