我认为有必要首先提及我对 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
变为 --open
,aria-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
点击后显示如下:
如有任何关于上述内容的指导,我们将不胜感激。
最佳答案
在之前的问题中,我发现调用控件上的焦点方法是实现点击所必需的问题。所以调用
hDropSelect.Elements("aria-expanded").focus
之前
hDropSelect.Elements("aria-expanded").Value = "true"
可能会有帮助。
关于html - 如何使用 VBA 单击网站上的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037741/