0

I am trying to open a site. In that site there is a download link which when clicked, causes a pop-pup named "File Download" to open which has 3 options: open, save and cancel where currently cancel is selected. If i click save then it opens another pop up where i have to click save. I am trying to automate this using vb script. Code:

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "www.abc.com"

Do While IE.Busy
    WScript.Sleep 100
Loop

Set Links = IE.document.getElementsByTagName("a")

For Each Link In Links

    If InStr(1, Link.href, "?task=downloadFile", vbTextCompare) > 0 Then

    IE.Navigate Link.href

    Do While IE.Busy
        WScript.Sleep 100
    Loop
Set WshShell = CreateObject("WScript.Shell")
WScript.Sleep 1000

WshShell.Sendkeys "{TAB}"
WScript.Sleep 1000
WshShell.Sendkeys "{TAB}"
WScript.Sleep 1000
WshShell.Sendkeys "{TAB}"
WScript.Sleep 1000
WshShell.Sendkeys "{TAB}"
WScript.Sleep 1000
WshShell.Sendkeys "{ENTER}"
WshShell.Sendkeys "{TAB}"
WScript.Sleep 1000
WshShell.Sendkeys "{ENTER}"

    Exit For
End If
Next

This code is not working as some random windows are getting selected. Kindly help

user2862496
  • 227
  • 5
  • 12
  • 21
  • 1
    When you go see the doctor, do you tell him "I'm sick" and let him guess what's wrong, or do you describe the symptoms ? Same thing here, please describe the symptoms... – Laurent S. Apr 29 '14 at 12:08
  • Let´s ask the other way around: Why should this code work at all? – TheBlastOne Apr 29 '14 at 12:44

1 Answers1

0

Forget about SendKeys - it's terribly unreliable, because other windows can steal focus at any time. Instead, use the Microsoft.XMLHTTP and ADODB.Stream objects to download your file, as shown in this answer.

Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314