1

I am trying to create IEautomation through vba-excel for the following link.

URL: http://qpldocs.dla.mil/search/default.aspx

The code includes search for the string "QPL-631",and click on the corresponding java script link MIL-I-631D(6).When I inspected "MIL-I-631D(6)" link ,I found following source code of href tag

<a href="javascript:__doPostBack('search_list$DG$ctl03$ctl00','')">MIL-I-631D(6)</a>

So there are no click options for the href link and the address of manual clicking on href link is completely different than href address.So I am stuck here.I would like to add a code that clicks "MIL-I-631D(6)" and outputs the results.

I have tried the below code and so far and unable to proceed further.

Private Sub IE_Autiomation()

Dim i As Long

Dim IE As Object

Dim objElement As Object

Dim objCollection As Object

Dim ae As HTMLLinkElement

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.navigate "http://qpldocs.dla.mil/search/default.aspx"
Application.StatusBar = "Loading. Please wait..."

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

Application.StatusBar = "Search form submission. Please wait..." IE.document.getElementById("Search_panel1_tbox").Value = "QPL-631" IE.document.getElementById("Search_panel1_btn").Click Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop

631 Link

Community
  • 1
  • 1
Neelesh
  • 138
  • 1
  • 10
  • Are you legally allowed to scrape this website? Can't see anything saying you can't in the info they give.... – QHarr Mar 01 '18 at 08:22
  • It is an open source website for knowing the approved manufacturers.So I believe I am allowed to. – Neelesh Mar 01 '18 at 08:25

2 Answers2

1

Here is an interim solution to write out to sheet as you are already hard coding the product code "QPL-631" you can just skip straight to using that in the URL string to return your results.

Note: I have pulled the table ID from that page:

html.getElementById("Lu_gov_DG")

You might want to explore if this is a common theme across products (I suspect yes). Will make life a lot easier. You could even do away with IE altogether and go for a faster XHR solution.

Option Explicit

Private Sub IE_Automation()

    'References Internet Controls and HTML Object library

    Dim i As Long
    Dim IE As Object
    Dim html As HTMLDocument

    Dim product As String
    product = "QPL-631"

    Dim url As String

    url = "http://qpldocs.dla.mil/search/parts.aspx?qpl=1528&param=" & product & "&type=256"

    Set IE = CreateObject("InternetExplorer.Application")

    With IE

        .Visible = True

        .navigate url                            '"http://qpldocs.dla.mil/search/default.aspx"

        Application.StatusBar = "Loading. Please wait..."

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

        Set html = .Document

        Dim allRowOfData As Object

        Set allRowOfData = html.getElementById("Lu_gov_DG")

        Dim r As Long, c As Long

        Dim curHTMLRow As Object

        For r = 1 To allRowOfData.Rows.Length - 1

            Set curHTMLRow = allRowOfData.Rows(r)

            For c = 0 To curHTMLRow.Cells.Length - 1
                Cells(r + 1, c + 1) = curHTMLRow.Cells(c).innerText
            Next c

        Next r

        .Quit

    End With

    Application.StatusBar = False 'And tidy up our change to the status bar

End Sub

There is example with postback here, which I will have a look at.

Reference:

  1. How to reset the Application.StatusBar
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Thanks.This will work only for QPL-631.What if I want to get the details of other QPL numbers? For instance QPL-38999,When I search for QPL-38999 and hit the `href` tag the url code would be `http://qpldocs.dla.mil/search/parts.aspx?qpl=1122&param=QPL-38999&type=256`.Just see the change of QPL number,it is now changed to **1122** in the address.So this is what I am interested to know.How is the `href` tag redirecting to unique QPL addresses and how to implement that into `vba` so that the code can search every QPL numbers. – Neelesh Mar 02 '18 at 06:09
  • Do you have a couple of other QPL numbers I can play around with please? – QHarr Mar 02 '18 at 06:44
  • Here you have,QPL-AMS2644,QPL-6081,QPL-6529,QPL-6645,QPL-7808 – Neelesh Mar 02 '18 at 07:41
  • So far this answer seems to imply that you can't access the re-direct URL as it is server side.....https://stackoverflow.com/questions/33377264/how-to-find-the-actionurl-in-a-javascript-dopostback – QHarr Mar 02 '18 at 11:18
0

You have several options available to you. See below for a pretty comprehensive list of possibilities.

Try getting the collection of anchor tags, with:

GetElementsByTagName("a")

Then, iterate that collection using as much logic as you can to ensure you're clicking the right button.

For each l in ie.document.getElementsByTagName("a")

If l.ClassName = "hqt_button" Then
    l.Click
    Exit For
Next

If there are multiple anchors with the same classname, you could do:

If l.ClassName = "hqt_button" AND l.Href = ""javascript:void(0): onclick=HeaderBox.trySubmit()" Then
    l.Click
    Exit For
Next

Alternatively

If you are using IE9+ you could use the GetElementsByClassName method.

GetElementsByClassName("hqt_button") 

How do I use excel vba to click a link on a web page

ASH
  • 20,759
  • 19
  • 87
  • 200
  • i think, but I don't know, that the website is an ASP.Net application. and the function __doPostBack() is simply posting the arguments back to the server which is then redirecting to the other page hence why I haven't got a directly clickeable link. Was hoping @omegastripes or timwilliams might have seen this. But if you are familiar with this scenario as well, I am keen to see a solution. I included the link to a similar question in the comments. – QHarr Mar 04 '18 at 06:57
  • @QHarr You are absolutely correct.@ryguy72 There are no `hqt button` arguments in the web page and hence the code posted by won't serve the purpose. – Neelesh Mar 06 '18 at 07:24
  • @Neelesh thanks for clarifying. Is the OP question solveable? I would be keen to learn how to work with this. I am guessing you have to use postman or some such to see what is being passed back and forth? – QHarr Mar 06 '18 at 08:12
  • Or use fiddler? – QHarr Mar 06 '18 at 08:19
  • 1
    @QHarr Unfotunately No.I am not sure how to deal with those. – Neelesh Mar 09 '18 at 07:46