0

I am trying to automate a file downloading from a website. When I do the download manually, all I have to do is to click on the "save" icon (floppy disk), then another window pops up for a second and the download begins (while the popped up window disappears).

What I usually do (when I automate a download) is to find the files URL, then I use the URLDownloadToFile function. But in this case I cannot find the url in the html. I tried to use the .click and FireEvent on the object but nothing worked.

So I started to think (based on similar question in this site) that a script generates the URL when I press the "save" icon. Unfortunately I am not familiar with javascript or how it works. Right now I am trying to use my browser's developer tools's console to figure out what happens when I click the object. BTW: this object is an <img> object.

I searched the web for answers, and I think that somehow I will have to call the javascript myself if I want to download the file, with something like execScript. But how do I find out which script gets called when I click on the icon? And more importantly will I be able to understand your answer without completely understanding how a webpage works? :)

P.S.: I know it would be far easier if I could give you the site address, but it requires a login to see the stuff that I am talking about...

MRK_S
  • 9
  • 1
  • 3
  • TBH I don't know how to help here. Is there no HTML to share? Something that narrows down the field. This doesn't feel at all [mcve]. My limited experience with js function calls is that it really helps to see it in situ and be able to work out the whole flow of the page . I did note your comment about login. – QHarr May 05 '18 at 20:26
  • @QHarr I edited my question, let me know how can I help more, without giving access to the actual site. – MRK_S May 05 '18 at 20:56
  • Is there anything that says image.setUrl ? – QHarr May 05 '18 at 21:05
  • In the HTML code there is none. And if I unwrap the EventListener a little bit (you can see it on the second printscreen I just added to the question) I see that "proto" function embedded into each other (infinitely - as I see). But inside there are stuffs like "set caller" and "set argument". – MRK_S May 05 '18 at 22:10
  • 1
    You may check using Fiddler if there is any XHR is made when you click and download the file manually in the browser, and if so, reproduce that XHR from VBA code then. – omegastripes May 06 '18 at 00:05
  • @omegastripes I think I can detect XHR-s with the default developer tools in chrome, right (under "Network")? I edited my post with a GIF, when I simulate a download while checking what happens on the website. – MRK_S May 06 '18 at 10:57
  • @MRK_S Studying the same issue I noted that Chrome developer tools network tab doesn't capture such requests for file downloading, but in the same time Fiddler works perfectly. – omegastripes May 06 '18 at 15:51
  • @omegastripes allright. I installed Fiddler 4 (from telerik - I hope that's what you were talking about). And when I initiate the download, I can see that there is some "traffic". But I don't know what to do with it. Could you give me some directions? I would like to share my results (what happens when I download the file in Fiddler), but right now I don't know what to share... And do you think this will enable me to download the file without having to actually click on the icon and somehow acces the file's URL directly? – MRK_S May 06 '18 at 20:38
  • @MRK_S First of all set up filter on the Filters tab to the right (by client process or by URL contains). Open logged request, click Inspectors tab to the right, you will see the GUI like Chrome devtools network tab: request is at the top, response - at the bottom. Click on response TextView tab (depends on file type) and make sure you have captured the right XHR, and there is the file you need. Then click on request Headers tab, and try to find out where you can get all that data. As a rule, you need to make XHR to get downloading page content, and extract that data prior to downloading. – omegastripes May 06 '18 at 21:09
  • @MRK_S Have you succeed with Fiddler? – omegastripes Sep 16 '18 at 21:14

2 Answers2

0

Assuming you are trying to download the image -
Have a look at How do I base64 encode a string efficiently using Excel VBA? There you'll find a Decode method. Use that on the base64 portion of the url, that is the text between &quot;data:image/png;base64, and &quot;. Save that as a binary file and you have your image.

Sam
  • 5,424
  • 1
  • 18
  • 33
  • Can you show in full how that would work with the above or do you need more HTML? – QHarr May 06 '18 at 09:18
  • That's very interesting, I never heard of this approach. But as you said, it would give me the image itself, not the file. I updated my post with a GIF of a simulated download on the site. I hope that helps. – MRK_S May 06 '18 at 10:59
  • Never mind. I just realized I misunderstood your problem description. The image in the url is the icon on the button, which I presume is of little interest. – Sam May 06 '18 at 14:46
0

Does this help?

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
  "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
    szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub DownloadFilefromWeb()
    Dim strSavePath As String
    Dim URL As String, ext As String
    Dim buf, ret As Long
    URL = Worksheets("Sheet1").Range("A2").Value
    buf = Split(URL, ".")
    ext = buf(UBound(buf))
    strSavePath = "C:\Users\rshuell\Desktop\Downloads\" & "DownloadedFile." & ext
    ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
    If ret = 0 Then
        MsgBox "Download has been succeed!"
    Else
        MsgBox "Error"
    End If
End Sub

Or....

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
  "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
    szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Private Sub pMain()
  Dim sURL As String
  Dim sDestination As String
  Dim bSuccess As Boolean
  Dim lRow As Long
  Dim ws As Excel.Worksheet
  Dim strSavePath As String
  Dim URL As String, ext As String
  Dim buf, ret As Long

  'Change to suit
  Set ws = ThisWorkbook.Worksheets("Sheet1")

  With ws
    For lRow = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
      sURL = .Cells(lRow, "A")
      sDestination = .Cells(lRow, "B")

      buf = Split(sURL, ".")
      ext = buf(UBound(buf))

        pos = InStrRev(sURL, "/", -1)
        file = Mid(sURL, pos + 1, 99)
        strSavePath = sDestination & file
        ret = URLDownloadToFile(0, sURL, strSavePath, 0, 0)
            If ret = 0 Then
                .Cells(lRow, "C") = "File download successfully!"
            Else
                .Cells(lRow, "C") = "Couldn't download the file!"
            End If

      DoEvents
    Next lRow
  End With
End Sub

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200
  • I knew about this method, but this would require me to know the file's URL. And that's the problem, that I don't know that... I think it's generated somehow when I click the icon (you can see this in action on the GIF in the end of my post). – MRK_S May 06 '18 at 13:54