使用VBA网页抓取在下拉菜单中select值
我有一个问题,尝试从JavaScript网页上的下拉菜单中select项目。 我的最终目标是通过用户窗体填写菜单值,但是我没有成功创buildVB来select下拉菜单。 网页代码如下
<select name="dateSelector0" class="clsInputArea selectBox valid" style="display: none; "onchange="setDateRange(this, 'rtf[0].val1', 'rtf[0].val2')"> <option value="-1"></option> <option value="1">Last Month</option> <option value="2">Current Month</option> <option value="3">Next Month</option> <option value="4">Last Year</option> <option value="5">Current Year</option> <option value="6">Next Year</option> <option value="7">First Quarter</option> <option value="8">Second Quarter</option> <option value="9">Third Quarter</option> <option value="10">Fourth Quarter </option></select> <a tabindex="NaN" title="" class="selectBox clsInputArea selectBox-dropdown" style="width: 147px; display: inline-block;" href="javascript:void(0);"><span class="selectBox-label" style="width: 127px;"> </span><span class="selectBox-arrow"></span></a>
我已经尝试了各种GetElementsBy,与上面的名字,尝试下面的名称和身份证rtf [0] .val1但无济于事,下面的例子。 我相信通过dateSelector0名称将是最好的,但我将不胜感激在这个比你更好的input。
ie.Document.Body.GetElementsByname("dateSelector0").Value = "1" ie.Document.Body.GetElementsByname("dateSelector0").Item(0).FireEvent ("onchange")
你几乎已经拥有了。 我刚刚试过:
Sub IE_Navigate() 'Declare Dim IE As Object 'Use IE Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.navigate ("Website URL") 'Wait for Load to finish While IE.readyState <> 4 DoEvents Wend Application.Wait (Now + TimeValue("0:00:01"))
这两种方法都适用于我。
通过ClassName从dropmenu中select数据:
IE.document.getElementsByClassName("clsInputArea")(0).Value = 1
要么
按名称从dropmenu中select数据
IE.document.getElementsByName("dateSelector0")(0).Value = 1
其中返回从下拉菜单中的第一个项目。
EDITED