Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用VBA web scrape在下拉菜单中拾取值_Java_Excel_Vba_Web Scraping - Fatal编程技术网

Java 使用VBA web scrape在下拉菜单中拾取值

Java 使用VBA web scrape在下拉菜单中拾取值,java,excel,vba,web-scraping,Java,Excel,Vba,Web Scraping,我在尝试从javascript网页的下拉菜单中选择项目时遇到问题。我的最终目标是通过用户表单填写菜单值,但我没有成功创建VB来选择下拉列表。网络代码如下 <select name="dateSelector0" class="clsInputArea selectBox valid" style="display: none; "onchange="setDateRange(this, 'rtf[0].val1', 'rtf[0].val2')"> <option val

我在尝试从javascript网页的下拉菜单中选择项目时遇到问题。我的最终目标是通过用户表单填写菜单值,但我没有成功创建VB来选择下拉列表。网络代码如下

<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;">&nbsp;</span><span class="selectBox-arrow"></span></a>

你差点就成功了。我刚才试过:

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"))

按名称从下拉菜单中选择数据

IE.document.getElementsByName("dateSelector0")(0).Value = 1
返回下拉菜单中的第一项


编辑

如果您观察以下CSS选择器,其中和表示选择该类元素内的所有“选项”标记(
clsInputArea selectBox valid
),您将看到它做出了正确的选择

注意:CSS选择器中不允许使用复合名称,这就是为什么类名称中的空格被替换为“.”

CSS查询结果示例:

Dim currentOption As Object
For Each currentOption In x
    If InStr(currentOption.innerText, "Last Month") > 0 Then
        currentOption.Selected = True
        Exit For
    End If
Next currentOption


VBA:

Dim currentOption As Object
For Each currentOption In x
    If InStr(currentOption.innerText, "Last Month") > 0 Then
        currentOption.Selected = True
        Exit For
    End If
Next currentOption
现在,我们可以将其转换为以下语法:

ie.Document.getElementsByClassName("clsInputArea selectBox valid")(0).getElementsByTagName("option")
这假设索引0是用于类“select_list”元素的正确索引。如果将集合设置为变量(例如

Dim x As Object
Set x = ie.Document.getElementsByClassName("clsInputArea selectBox valid")(0).getElementsByTagName("option")
假设对象不是空的,那么我们可以通过其索引(即位置)选择一个选项,或者通过循环集合并在满足特定条件(例如,其内部文本与所需短语匹配)时进行选择

按索引选择:

Dim currentOption As Object
For Each currentOption In x
    If InStr(currentOption.innerText, "Last Month") > 0 Then
        currentOption.Selected = True
        Exit For
    End If
Next currentOption
这意味着你可能会说

x.SelectedIndex=1
选择
上月

通过循环选择:

Dim currentOption As Object
For Each currentOption In x
    If InStr(currentOption.innerText, "Last Month") > 0 Then
        currentOption.Selected = True
        Exit For
    End If
Next currentOption

选择下拉列表的完整示例:

Dim currentOption As Object
For Each currentOption In x
    If InStr(currentOption.innerText, "Last Month") > 0 Then
        currentOption.Selected = True
        Exit For
    End If
Next currentOption
由于您不提供URL,下面是一个使用不同网站的示例。网页目标下拉列表,
Código de Ativo
,如下所示:

要选择的目标选项是编号为3的
AALR21
,或基于0的索引的项目2

项目innertext上的循环和匹配
AAL21

Option Explicit
Public Sub MakeSelectiong()
    Dim IE As New InternetExplorer
    Const URL = "http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/"
    Const optionText As String = "AALR21"        'Number2
    Application.ScreenUpdating = False           '   a.selectedIndex = 2

    With IE
        .Visible = True
        .navigate URL
        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim a As Object, currentOption As Object
        Set a = .document.getElementById("ctl00_ddlAti")

        For Each currentOption In a.getElementsByTagName("Option")
            If InStr(currentOption.innerText, optionText) > 0 Then
                currentOption.Selected = True
                Exit For
            End If
        Next currentOption
        Stop
        .Quit
    End With
End Sub

谢谢Hubvill!然而,这对我来说是行不通的。我用的是IE 11,你认为这是个问题吗?我尝试了使用fireevent onchange line和不使用fireevent onchange line。我正在使用IE9,但在更改IE版本之前,我会检查其他事项。您是否有代码检查网页是否已完成加载?例如:'等待加载完成,而IE.readyState 4不存在事件。您还需要检查它们是否是源代码中的任何其他名称=“dateSelector0”。getElementsByName(“dateSelector0”)(0)将只找到包含该名称的第一个标记。因此,例如,如果有两个下拉菜单的名称为=“dateSelector0”,并且您希望在第二个下拉菜单中输入数据,则使用getElementsByName(“dateSelector0”)(1)。值=1是,我在ie.busy do events循环时做过,我尝试将其更改为readystate 4,但它现在给了我一个运行时438错误:对象不支持fireevent行的此属性或方法还有一个DateSelector 1,但我打算将它们都填写出来。第一个日期下拉列表是dateselector0,在我转到其他字段之前,我一直在尝试使用它,因为我认为这将是非常类似的编码,只是修改名称/id