使用VBA点击javascript href链接

时间:2016-11-09 21:59:44

标签: javascript excel-vba href vba excel

我正在尝试自动化一个程序,该程序查找特定地块的物业纳税申报表。我用来这样做的网站是here

在此示例中,我正在查找的宗地ID为648-30-013。

当我搜索该包裹时,我到达了一个页面,其中包含我需要点击的链接。该链接的html如下:

<a href='javascript:SubmitThisForm("General.asp", "64830013");'>648-30-013</a>

我已尝试使用以下代码点击该链接,但尚未成功。我遇到问题的部分是最后一部分'点击包裹ID链接。代码在步进时工作正常,但在执行时,objCollection变量不会填充。

'Declare Variables

' Counter variable
Dim i As Integer

' Internet explorer variables
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim lookupURL As String

' Excel variables
Dim currentParcel As String
Dim year As Integer
Dim jobs As String
Dim completedLoops
Dim totalLoops

' Input variables
'These are the counter variables that loop through your lookup data; where to start and end and how long to loop for
completedLoops = 0
totalLoops = 1
lookupURL = "http://fiscalofficer.cuyahogacounty.us/AuditorApps/real-property/REPI/default.asp"


'Set value of current lookup based on a starting value and # of loops completed
currentParcel = "648-30-013"
Application.StatusBar = "Executing " & completedLoops & " of " & totalLoops & " loops"


' Establish Internet Exploere Instance
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

' Navigate to website
IE.Navigate lookupURL

' Wait while IE loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

' Click on 'By Parcel #' tab
' Create a list of all the <div> tags on the page
Set objCollection = IE.document.getElementsByTagName("div")

' Loop through all <div> tags to find the one with the correct name and input current search term into tag
i = 0
Do While i < objCollection.Length
    If objCollection(i).ID = "tabTabdhtmlgoodies_tabView2_1" Then
        Set objElement = objCollection(i)
        objElement.Click
        Exit Do
    End If
    i = i + 1
    Loop

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop


' Input parcel ID
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
Do While i < objCollection.Length
    If objCollection(i).Name = "parcelNum" Then
        objCollection(i).Value = currentParcel
        Exit Do
    End If
    i = i + 1
    Loop

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

' Click on search button
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
Do While i < objCollection.Length
    If objCollection(i).Name = "b_2" Then
        Set objElement = objCollection(i)
        objElement.Click
        Exit Do
    End If
    i = i + 1
    Loop

Application.Wait DateAdd("s", 5, Now)


' Click on parcel ID link
Set objCollection = IE.document.getElementsByTagName("a")
Debug.Print objCollection.Length
i = 0
Do While i < objCollection.Length
    If objCollection(i).innerText = currentParcel Then
        Debug.Print objCollection(i).innerText
        Set objElement = objCollection(i)
        objElement.Click
        Exit Do
    End If
    i = i + 1
    Loop

如果有更好的方法可以点击此链接,请告诉我。

1 个答案:

答案 0 :(得分:0)

这对我有用。

如果您正在进行任何数量的网页抓取,那么抽出尽可能多的&#34; guts&#34;因为你可以使用实用的方法,你可以重复使用它们,这有助于消除主要代码的混乱,这样你就可以更容易地专注于&#34;什么&#34;与&#34;如何&#34;。

master = nil
my_array.each do |user|
  if !master
    master = user
  else
    user.attributes.each do |k, v|
      if v.present? && !master.send(k).present?
        master.send(:"#{k}=", v)
      end
    end
  end
end