使用VBA控制IE中的组合框元素

问题描述 投票:0回答:1

我正在尝试使用excel / VBA自动从网站进行数据转储,我一直试图从组合框中选择一个选项。我已经使用DOM Explorer查找其类名和ID,但没有任何效果。

这里是来自DOM Explorer的信息:

<select class="rccontrol" id="hyperfindid" rccinfo="headerParam:Query_Name">

我尝试使用:

.document.getElementById("hyperfindid") ' This does not return an object .document.getElementByClassName("rccontrol") ' This returns an object but i cant do anything with it?

我只想能够从此组合框中选择

有人可以给我一些想法吗?

问候,

internet-explorer automation
1个回答
0
投票

请参考以下代码,使用getElementsByClassName或getElementbyId方法获取选择元素:

Public Sub ClickTest()    
    Dim  ie As Object         
    Dim item As Object    
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend

        Set item = ie.Document.getElementsByClassName("rccontrol")(0)

        'Default selected value
        expCcy = "saab"

        For Each o In item             
            'print the value and innerText to check the value.
            'Debug.Print o.Value
            'Debug.Print o.innerText

            If o.Value = expCcy Then
                o.Selected = True
                Exit For
            End If
        Next
        'we could also use the following code to find the select element. 
        'ie.Document.getElementbyId("hyperfindid").Focus
        'ie.Document.getElementbyId("hyperfindid").selectedIndex = 2

        'if the select elements have javascript change event, we could use the following code to trigger the javascript change event
        'ie.Document.getElementbyId("list").FireEvent ("onchange")
    End With
   Set ie = Nothing

End Sub

网站资源如下:

<select class="rccontrol" id="hyperfindid">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
© www.soinside.com 2019 - 2024. All rights reserved.