如何使用VBA在网站上单击组合框

时间:2019-07-15 10:21:12

标签: html vba internet-explorer web-scraping

我认为必须首先提到我对HTML的使用经验从零到零。话虽这么说,我一直在努力整理我在互联网上找到的信息,但似乎无济于事。

问题:

我正在尝试单击给定网站上的组合框。

comboxbox

我知道组合框是这样嵌入在HTML中的:

<body id="bodyTag" class="mo-brand-lumina"
 <form method="post" id="form1"
  <div id="main-container"
   <div class="main-content"
    <div id="data-manager"
     <div id="data-definition-select-container"
      <span class="select2 select2-container select2-container --default valid select2-container --below select2-container --focus
       <span class="selection"
        <span class="select2-selection select2-selection--single role="combobox" aria-expanded="false"

重要观察:单击下拉列表时,--focus更改为--open,而aria-expanded更改为true

现在,我的尝试。我创建了oIE对象,可以登录网页,但是似乎无法单击组合框。显然这是由于缺乏了解。下面是我的VBA,我尝试点击该按钮。我尝试使用.GetElementsByClassName.getElementById.Elements.getElementsByName。显然是错误的(由于我不理解!)。

Option Explicit

Sub OpenWebPage()

    Dim oIE As Object
    Dim sURL As String
    Dim HTML As HTMLDocument, hDataManager As IHTMLElementCollection, hDropSelect

    Set oIE = CreateObject("InternetExplorer.Application")

'//---Open the browser and log in, then navigate to the data manager
    sURL = "https://publicisuk.demo.lumina.mediaocean.com/Admin/DataManager.aspx"

    oIE.Silent = True 'No pop-ups
    oIE.Visible = True
    oIE.Navigate sURL

    'wait for process to complete before executing the next task
    Do While oIE.Busy: DoEvents: Loop
    Do Until oIE.ReadyState = 4: DoEvents: Loop

    oIE.Document.Forms(0).All("LoginUsername").Value = "myUsername"
    oIE.Document.Forms(0).All("LoginPassword").Value = "myPassword"
    oIE.Document.Forms(0).Submit

    'wait for process to complete before executing the next task
    Do While oIE.Busy: DoEvents: Loop
    Do Until oIE.ReadyState = 4: DoEvents: Loop

'//---Start with vendor
    Set HTML = oIE.Document

'    Set hDataManager = HTML.getElementById("data-manager")
'    Set hDropSelect = hDataManager.getElementByClassName("select2-selection select2-selection--single")
    Set hDropSelect = HTML.getElementsByClassName("select2-selection select2-selection--single")

    hDropSelect.Elements("aria-expanded").Value = "true"


'    oIE.Document.getElementById("select2-DataDefinitionAutoComplete-container").Click

'    oIE.Document.Forms(0).All("DataDefinitionAutoComplete").Click

'    oIE.Documents.Forms("data-manager").Elements("data-detail-container").Value = "Vendor"
'    oIE.Document.getElementsByName("SubscribeButton")(0).Click



    Set oIE = Nothing

End Sub

单击后显示以下内容:

dropdown

任何有关上述内容的指导将不胜感激。

2 个答案:

答案 0 :(得分:0)

在上一个问题中,我发现在控件上调用focus方法是一个问题,才能实现单击。所以打电话

hDropSelect.Elements("aria-expanded").focus

之前

hDropSelect.Elements("aria-expanded").Value = "true"

可能会提供帮助。

答案 1 :(得分:0)

由于无法详细复制上面的示例,我可以理解缺乏响应。

我设法使用.Focus.innerText.SendKeys的组合来找到可行的解决方案,以获得期望的结果,尽管.SendKeys绝不是最好的方法。

我用过:

With oIE.document
    'set focus
    .getElementsByClassName("select2-selection__rendered")(0).Focus
    'send enter to trigger dropdown
    Application.SendKeys "~", True

    'now get dropdown box
    .getElementsByClassName("select2-search__field")(0).innerText = "Vendor"
    'send trigger
    Application.SendKeys "~", True

    'subscribe button - this works
'   With .querySelector("#SubscribeButton")
'       .Click
'   End With
End With