【问题标题】:How to click a combobox on a website using VBA如何使用 VBA 在网站上单击组合框
【发布时间】:2019-07-15 10:21:12
【问题描述】:

我认为有必要首先提到我使用 HTML 的经验为零甚至没有。话虽如此,我一直在尝试将我在互联网上找到的信息拼凑起来,但似乎没有任何帮助。

问题:

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

我知道组合框是这样嵌入在 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 更改为--openaria-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

点击后显示如下:

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

【问题讨论】:

  • 为什么需要点击组合框?您能否描述一下背景以及您一般要完成的任务是什么?
  • @omegastripes 当然可以。我需要单击组合框,然后输入“供应商”,这将显示数据库中的当前供应商。然后我需要以 Excel 格式导出它,然后我将使用它从中提取信息。我已经评估过使用 PowerQuery 建立数据连接,但这不起作用。该公司也不为他们的数据库提供 API,所以我的解决方案是前端网络爬虫。
  • 问题是由于需要注册,其他人无法重现该问题。我建议您选择 HTTP 请求而不是 IE。
  • 组合框上向下箭头的 html 是什么?
  • 看看这里的讨论。 stackoverflow.com/questions/44545772/…。看起来该页面正在使用来自 jQuery 的 Select2 控件。您也许可以通过 myDoc.parentWindow.execScript "$('MyCssSelectorForSelectListGoesHere').val('TheValueIWantToSet').trigger('change')""$('MyCssSelectorForSelectListGoesHere').val('TheValueIWantToSet').change() 之类的东西逃脱惩罚"

标签: html vba internet-explorer web-scraping


【解决方案1】:

在上一个问题中,我发现调用控件上的焦点方法是实现单击所必需的问题。所以调用

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

之前

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

可能会有帮助。

【讨论】:

    【解决方案2】:

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

    我设法使用.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多