SIM
SIM

Reputation: 22440

Ho to fix my wrongly cycling loop in vba?

I've written a script to get data from a certain website using reverse search with my vba script which has been written in combination with selenium. It is working good for the first search but when the first loop goes for the second item it takes A1 again instead of A2 and the second loop picks the right item which is B2 in this case. To be more specific, first time it takes A1, B1 but second time it takes A1, B2 in lieu of A2, B2. How can i fix this loop so that it picks items parallelly. Here is what i was trying with:

Sub HCAD()
    Dim driver As New ChromeDriver, post As Range, elem As Range
    For Each post In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        For Each elem In Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row)
            With driver
                .Get "http://hcad.org/quick-search/"
                .Wait 500
                .SwitchToFrame .FindElementByTag("iframe")
                .FindElementById("s_addr").Click
                .FindElementByName("stnum").SendKeys (post)
                .FindElementByName("stname").SendKeys (elem)
                .FindElementByXPath("//input[@value='Search']").Click
                .Wait 1000
            .SwitchToFrame .FindElementByTag("iframe")
            i = i + 1: Cells(i, 3) = .FindElementByXPath("/html/body/table/tbody/tr/td/table[5]/tbody/tr[2]/td[1]/table/tbody/tr/th").Text
            End With
        Next elem
    Next post
    driver.Quit
End Sub

Btw, the search criteria were:

A1. 8227  B1. FINDLAY ST
A2. 6330  B2. LAUTREC DR

Upvotes: 0

Views: 95

Answers (1)

jsotola
jsotola

Reputation: 2278

it is the way your loops are nested

with the 2 rows, you run 4 searches ... A1B1 .. A1B2 .. A2B1 .. A2B2

use only one loop on column A ("post" loop)

change second sendkeys to: .FindElementByName("stname").SendKeys (post.offset(0,1))

Upvotes: 2

Related Questions