如何通过excel vba选择网页中的特定下拉元素

时间:2018-09-24 10:59:29

标签: excel-vba drop-down-menu

目前,我正在一个网页上提取与我的工作相关的数据。在生产列表中选择下拉菜单时,我在这里遇到问题。我想在下拉列表中选择“全部”选项。默认情况下,生产表中仅包含25个值。 这是我的代码

Sub Production_table()

    Dim objIE As InternetExplorer
    Dim ele As Object
    Dim ele2 As Object
    Dim ele3 As Object
    Dim s As String
    Dim i As Integer

    i = 2

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Do While Range("A" & i).Value <> ""

    Set objIE = New InternetExplorer
    objIE.Visible = True
    source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search
    objIE.navigate source"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    Sheets.Add
    ActiveSheet.Name = "Data"
    On Error Resume Next

    Set ele2 = objIE.document.getElementById("productionTable_length"). _
      getElementsByTagName("label")

    For Each ele3 In ele2
    If ele3.Value = -1 Then
    ele3.Focus
    ele3.Selected = True
    ele3.Click
    Exit For
    End If
    Next

网页链接-https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search

请帮助我。

1 个答案:

答案 0 :(得分:1)

在这种情况下,我总是建议您使用Chrome浏览器,因为它可以让您以一种用户友好的方式检查元素。

enter image description here

尝试一下

Sub Sample()
    Dim objIE As Object, ele As Object, opt As Object

    Set objIE = CreateObject("InternetExplorer.Application")

    objIE.Visible = True
    Source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
    objIE.navigate Source

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    Set ele = objIE.document.getElementsByTagName("select")

    For Each opt In ele
        If opt.getAttribute("name") = "tblWellRecords_length" Then
            opt.Focus
            opt.Value = -1
            Exit For
        End If
    Next opt
End Sub
  

我一直在寻找“全部”元素来点击生产数据。如果我将属性名称固定为“ productionTable_length”,它将更改下拉列表的值,但不会单击该元素。如果单击,则显示的总数将从1-25更改为1-166。请帮帮我。...– 2个小时前的帕塔

您没有说这句话:)您的问题是how to select a specific dropdown element in webpage by excel vba For which I gave you the answer。对于您想要的东西,有另一种方法。

尝试一下

Sub Sample()
    Dim objIE As Object, source As String, script As String

    Set objIE = CreateObject("InternetExplorer.Application")

    objIE.Visible = True
    source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
    objIE.navigate source

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    script = "$('select[name=productionTable_length]').val('-1'); $('select[name=productionTable_length]').trigger('change');"
    objIE.document.parentWindow.execScript script, "jscript"
End Sub