Sign in to follow this  
Followers 0
pete1234

IE - Click button and download file

4 posts in this topic

I am working with a form in IE. There is a download button that, when clicked, generates a file to download. I am trying to automate this process and am having some problems.

The code I have below is working and doing what I want. It clicks the button and saves the file to a location that I specify.

; the code before this is not relevant
$sldDownloadBtn = _IEFormElementGetObjByName($sldDownloadForm, "Button1")
_IEAction($sldDownloadBtn, "click")

WinWaitActive("File Download - Security Warning")
WinActivate("File Download - Security Warning")
If @OSVersion = "Win_VISTA" Then ControlClick("File Download - Security Warning", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("File Download - Security Warning", "", "[CLASS:Button; INSTANCE:2]")

WinWaitActive("Save As")
WinActivate("Save As")
ControlSetText("Save As", "", "[CLASS:Edit; INSTANCE:1]", "c:\fakefile123.xls" )
If @OSVersion = "Win_VISTA" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:2]")
$i = 1
; Check to see if the file was downloaded
While 1
    If FileExists("C:\fakefile123.xls") Then ExitLoop
    Sleep(1000)
    $i = $i + 1
    If $i > 240 Then
        MsgBox(0, "", "File not downloaded!")
        _IEQuit($oIE)
        Exit
    EndIf
WEnd
_IEQuit($oIE)
msgbox(0, "", "Success")

The problem is this works off window titles, and ultimately I need the program to run in the background. When I run the program with IE hidden the program stops at the call to WinWaitActive. Is there a way to find the URL that is generated after the button click? I could then use InetGet and avoid all this stuff with windows. Any thoughts??

FWIW this is the code attached to the button on the website

<A href="javascript:document.forms[0].submit();"><input type="reset" value="Clear Form" name="reset">&nbsp;</A><A href="javascript:document.forms[0].submit();"></A><INPUT id="bBDFText" type="submit" value="Build Data File!" name="Button1">

Thanks so much for any insight

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You could always try WinExists first, then activate. See if that helps.

; the code before this is not relevant
$sldDownloadBtn = _IEFormElementGetObjByName($sldDownloadForm, "Button1")
_IEAction($sldDownloadBtn, "click")

$count = 0
While 1
Sleep(100)
$count += 1
If WinExists("File Download - Security Warning") Then ExitLoop
If $count > 10 Then ;infinite loop protection, wait time is $x * 100 ms
$count = 0
_IEAction($sldDownloadBtn, "click")
EndIf
WEnd
;WinWaitActive("File Download - Security Warning")
WinActivate("File Download - Security Warning")
If @OSVersion = "Win_VISTA" Then ControlClick("File Download - Security Warning", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("File Download - Security Warning", "", "[CLASS:Button; INSTANCE:2]")

WinWaitActive("Save As")
WinActivate("Save As")
ControlSetText("Save As", "", "[CLASS:Edit; INSTANCE:1]", "c:\fakefile123.xls" )
If @OSVersion = "Win_VISTA" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:2]")
$i = 1
; Check to see if the file was downloaded
While 1
    If FileExists("C:\fakefile123.xls") Then ExitLoop
    Sleep(1000)
    $i = $i + 1
    If $i > 240 Then
        MsgBox(0, "", "File not downloaded!")
        _IEQuit($oIE)
        Exit
    EndIf
WEnd
_IEQuit($oIE)
msgbox(0, "", "Success")
Edited by Affe

[center][/center]

Share this post


Link to post
Share on other sites

Thanks for the quick reply Affe. Unfortunately that won't work when you run IE in hidden mode since the Window will never exist and can't be activated. I need some way to return the URL that is generated via the button click to a variable and then pass that to INetGet, but I can't find any info about how to do this.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks for the quick reply Affe. Unfortunately that won't work when you run IE in hidden mode since the Window will never exist and can't be activated. I need some way to return the URL that is generated via the button click to a variable and then pass that to INetGet, but I can't find any info about how to do this.

Hi Pete1234

I am looking similar solution in regards to this. Do you manage to sort out this cause one of the requirement has to save as to local disc? This based on the link on IE and it open up standard window - SAVE AS.

In my case, once the user click the link, in the background it's opening up another browser and trying to open up "File Security" but suddenly it close the window and the au3 application. Never pass to the next line ?!?!

You basically mentioned about IE in hidden mode ... what does this means?

I am appreciated if you could shared the finding.

Thanks

Edited by DCAlliances

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0