• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Trouble clicking on a search button using IE

shahin

Active Member
I've written a macro to parse some information from a webpage. There are two iframes in that webpage to hurdle with. When the browser leads to the landing page it is necessary to switch the first "iframe" to be able to click on the "search by address" button. I've already switched that and then a box appears with two search fields: one is "street number" and the other is "street name". I've already filled in the two search fields. Now, when i click on the search button to reach the target page the browser no longer performs any activity. Here is an iframe attached to this "search" button which needs to be switched as well otherwise the button will not response. I'm totally stuck here. How can i click on this button and enter the target page? FYI, I've already caught that iframe and set it in a variable which is commented out in the below script now. I've attached two images (marked with pencils) to show what I meant so far if anything is unclear at this point. Thanks in advance.

Here is what I've written so far:
Code:
Sub Hcad_Data()

    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim elem As Object, frm As Object
   
    With IE
        .Visible = True
        .navigate "http://hcad.org/quick-search/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set elem = .document.getElementsByTagName("iframe")(0)
        .navigate elem.src         ''switching to another page to be able to click on the "search by address" button
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With
   
    html.getElementById("s_addr").Click
    Application.Wait (Now + TimeValue("0:00:05"))
   
    html.getElementsByName("stnum")(0).Value = "8227"
    html.getElementsByName("stname")(0).Value = "FINDLAY ST"
    Do Until IE.readyState = READYSTATE_COMPLETE: Loop
   
    html.getElementsByTagName("input")(3).Click           ''no issues are there until this line
'    Do Until IE.readyState = READYSTATE_COMPLETE: Loop
'    Set frm = html.getElementById("quickframe")          ''this is the iframe
    Application.Wait (Now + TimeValue("0:00:05"))
   
'    IE.Quit
End Sub
 

Attachments

  • first page.jpg
    first page.jpg
    20.5 KB · Views: 6
  • second page.jpg
    second page.jpg
    25.8 KB · Views: 5
You are clicking on wrong Element...

Try...
Code:
html.getElementsByTagName("input")(8).Click

How to find the right element.
By looking at source code. You will notice that button has no name, but has Type="Submit". You can loop through the elements using something like...
Code:
    Dim iObj As Object
   
    Set iObj = html.getElementsByTagName("input")
   
    For i = 0 To iObj.Length - 1
            Debug.Print i & " " & iObj(i).Type & " " & iObj(i).Name
    Next

This returns...
Code:
0 submit
1 submit
2 submit
3 hidden search
4 hidden search
5 hidden search
6 text stnum
7 text stname
8 submit

Since you know that it comes after "stname", it must be 8.
 
How could you know everything sir!!! It solves the problem. Can you provide me with any link or idea where there are solutions on iframe related issues while dealing with IE? Thanks a lot once again sir.
 
It's matter of reading the source HTML and understanding it's structure. There's no comprehensive solution that I know of. Just learn by trial and error.

Often, before writing final code. I tend to loop through each Element with certain tag, and understand which one I'm really interested in or need and how to access it.

Use Immediate Window (& Debug.Print), Watch Window and Locals Window to good effect.
 
A little input with sir chihiro's answer. This one also works. I don't know whether it is an efficient way to deal with when I wanna ignore the indexing, as in `(8) in this case:
Code:
    For Each post In html.getElementsByTagName("input")
        If post.Value = "Search" Then
            post.Click
            Exit For
        End If
    Next post
 
It seems the document in the target page are within iframe that is because I can't scrape anything from that page even if my scraper can reach there. However, this is what I've written so far.

Code:
Sub Hcad_Data()

    Dim ie As New InternetExplorer, html As HTMLDocument
    Dim elem As Object, post As Object, info As Object
   
    With ie
        .Visible = True
        .navigate "http://hcad.org/quick-search/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set elem = .document.getElementsByTagName("iframe")(0)
        .navigate elem.src
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With
   
    html.getElementById("s_addr").Click
    Application.Wait (Now + TimeValue("0:00:05"))
   
    html.getElementsByName("stnum")(0).Value = "8227"
    html.getElementsByName("stname")(0).Value = "FINDLAY ST"
    Do Until ie.readyState = READYSTATE_COMPLETE: Loop
   
    For Each post In html.getElementsByTagName("input")
        If post.Value = "Search" Then post.Click: Exit For
    Next post
    Application.Wait (Now + TimeValue("0:00:05"))
      
    For Each info In html.getElementsByTagName("th")
        If InStr(info.innerText, "PADILLA ISAI & JOSEFINA") > 0 Then MsgBox info.innerText
    Next info

    ie.Quit
   
End Sub

Btw, any information from that target page will suffice. Thanks in advance.
 
It seems I've solved the issue. This was the first time for me to work with irame that is why I was in a great trouble to get around it. However, here is the working one:

Code:
Sub Hcad_Data()

    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim elem As Object, frm As Object, post As Object, info As Object
  
    With IE
        .Visible = True
        .navigate "http://hcad.org/quick-search/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set elem = .document.getElementsByTagName("iframe")(0)
        .navigate elem.src                               ''Switching to iframe
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With
  
    html.getElementById("s_addr").Click
    Application.Wait (Now + TimeValue("0:00:05"))
  
    html.getElementsByName("stnum")(0).Value = "8227"
    html.getElementsByName("stname")(0).Value = "FINDLAY ST"
    Do Until IE.readyState = READYSTATE_COMPLETE: Loop
  
    For Each post In html.getElementsByTagName("input")
        If post.Value = "Search" Then post.Click: Exit For
    Next post
    Application.Wait (Now + TimeValue("0:00:05"))
  
    Set frm = html.frames("quickframe").document   ''Reading documents from iframe
  
    For Each info In frm.getElementsByTagName("th")
        If InStr(info.innerText, "PADILLA ISAI & JOSEFINA") > 0 Then MsgBox info.innerText
    Next info
    IE.Quit
End Sub

The above script can parse data from the target page now.
 
Hi !

Easier just respecting object model !
Now you understand better why I always advise for
an observation before writing any codeline …
 
Back
Top