【发布时间】: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 更改为--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
点击后显示如下:
任何关于上述内容的指导将不胜感激。
【问题讨论】:
-
为什么需要点击组合框?您能否描述一下背景以及您一般要完成的任务是什么?
-
@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