html - 如何使用 VBA 单击网站上的组合框

标签 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 变为 --openaria-expanded 变为

现在,我的尝试。我已经创建了 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

如有任何关于上述内容的指导,我们将不胜感激。

最佳答案

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

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

之前

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

可能会有帮助。

关于html - 如何使用 VBA 单击网站上的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037741/

相关文章:

javascript - JQuery IE问题

javascript - 启用后关注html元素

javascript - 以编程方式触发文本光标,就像用户实际单击输入标签一样

excel - 如何在Excel中将表格转换为3列

vba - 如何获取连续最后一个非空单元格的值?

excel - 使用 mmult 时 VBA 中出现类型不匹配错误

internet-explorer - 如何防止IE对话框 "Always Ask Before Opening This Type of Address"打开?

css - Extjs 可调整大小的句柄颜色变化?

javascript - 将表单字段转换为 JSON 对象

javascript - jQuery uidatepicker 按钮图像不起作用