Управление элементом комбинированного списка в IE с помощью VBA - PullRequest
0 голосов
/ 04 октября 2019

Я пытаюсь автоматизировать дамп данных с веб-сайта, используя Excel / VBA, я застрял при попытке выбрать опцию в выпадающем списке. Я использовал DOM Explorer, чтобы найти его имя и идентификатор класса, но ничего не работает.

Вот информация из 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?

Я просто хочу иметь возможность выбрать из этого поля со списком

Может кто-нибудь дать мне некоторое представление?

С уважением,

1 Ответ

0 голосов
/ 04 октября 2019

Пожалуйста, обратитесь к следующему коду, чтобы использовать метод getElementsByClassName или getElementbyId для получения элемента select:

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