1
Sub Get_Data()


Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.scramble.nl/military-database/usaf"
Do While ie.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop
SendKeys "03-3114"
SendKeys "{ENTER}"


End Sub

The code below searches for keyboard typed value 03-3114 and gets a data in the table. If I 'd like to search for value which is already in cell A1 and scrape values from table for "Code, Type, CN, Unit" in cell range ("B1:E1") what should I do?

Community
  • 1
  • 1

1 Answers1

0

You are using SendKeys which are highly unreliable :) Why not find the name of the textbox and the search button and directly interact with it as shown below?

Sub Get_Data()
    Dim ie As Object, objInputs As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate "http://www.scramble.nl/military-database/usaf"

    Do While ie.readystate <> 4: DoEvents: Loop

    '~~> Get the ID of the textbox where you want to output
    ie.Document.getElementById("serial").Value = "03-3114"

    '~~> Here we try to identify the search button and click it
    Set objInputs = ie.Document.getElementsByTagName("input")
    For Each ele In objInputs
        If ele.Name Like "sbm" Then
            ele.Click
            Exit For
        End If
    Next
End Sub

Note: To understand how I got the names serial and sbm, refer to the explanation given just above the image below.

The code below searches for keyboard typed value 03-3114 and gets a data in the table. If I 'd like to search for value which is already in cell A1 and scrape values from table for "Code, Type, CN, Unit" in cell range ("B1:E1") what should I do?

Directly put the value from A1 in lieu of the hardcoded value

ie.Document.getElementById("serial").Value = Sheets("Sheet1").Range("A1").Value

To get the values from the table, identify the elements of the table by right clicking on it in the browser and clicking on "Inspect/Inspect Element(In Chrome it is just Inspect)" as shown below.

enter image description here

I can give you the code but I want you to do it yourself. If you are still stuck then update the question with the code that you tried and then we will take it from there.

Interesting read: html parsing of cricinfo scorecards

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Great hint! @ Siddharth Rout , I am having a hard time to import Code value from the web table (tr.rowBoard) into B1 cell in excel, I think Xpath is a problem but the xpath //*[@id="wideContent"]/div[5]/div[2]/div[1]/table/tbody/tr[3]/td[3]/span I got from inspect elements in Chrome – Dariusz Jezewski Aug 24 '16 at 12:10