Automating a tricky file upload dialog

Information related to the use of iMacros for form filling and data upload.
Post Reply
Tom, Tech Support
Posts: 3513
Joined: Mon May 31, 2010 4:59 pm

Automating a tricky file upload dialog

Post by Tom, Tech Support » Wed Apr 06, 2011 8:32 am

There are many cases where a web page presents a file input field for entering/selecting a file to upload. In these cases, it is very easy to use a simple TAG command for the input field and specify the appropriate file in the CONTENT parameter, as shown in the Upload.iim demo macro that ships with iMacros.

However, there are also cases where an input field is not provided on the web form (or it is not directly editable), and you can only choose the file by using a file upload dialog, which is usually presented after clicking a button or link. There are even cases where automatic click-mode does not work for recording the click on the upload button, and you must use DirectScreen or IMAGECLICK to click the button, as in the following example.
Tricky File Upload Dialog.png
The problem is, once you are presented with the file upload dialog, how do you automate it to specify the file you want to upload?

The answer is: use the DS CMD=KEY command after the dialog appears to send the path/name of the file and press Enter.

The following macro demonstrates just how to do this for the example page shown above:

Code: Select all

VERSION BUILD=7221254
TAB T=1
TAB CLOSEALLOTHERS
SIZE X=800 Y=867
URL GOTO=http://kleinanzeigen.ebay.de/anzeigen/p-anzeige-aufgeben.html
TAG POS=1 TYPE=A FORM=ID:pstctgry-frm ATTR=ID:cat_195
TAG POS=1 TYPE=A FORM=ID:pstctgry-frm ATTR=ID:cat_275
TAG POS=1 TYPE=BUTTON:SUBMIT FORM=ID:pstctgry-frm ATTR=TXT:Weiter
TAG POS=1 TYPE=INPUT:RADIO FORM=ID:adForm ATTR=ID:adType1&&VALUE:OFFER CONTENT=YES
TAG POS=1 TYPE=INPUT:RADIO FORM=ID:adForm ATTR=ID:posterType1&&VALUE:PRIVATE CONTENT=YES
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:adForm ATTR=ID:postad-title CONTENT=Italien<SP>-<SP>Ferienwohnung<SP>mit<SP>Garten<SP>-<SP>4<SP>Personen<SP>max.
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:adForm ATTR=ID:pstad-price CONTENT=290
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:adForm ATTR=ID:pstad-zip CONTENT=80331
TAG POS=1 TYPE=TEXTAREA FORM=ID:adFormBottom ATTR=ID:pstad-descrptn CONTENT=blah<SP>blah<SP>blah

' Click the file upload button
DS CMD=CLICK X=320 Y=517

' Wait a second to give the dialog a chance to pop up
WAIT SECONDS=1

' Send the path/name of the file and Enter to the dialog
DS CMD=KEY CONTENT=C:\Temp\Current.png{ENTER}
Note: This method will only work if the upload dialog does not block further execution of the macro. For example, if the file upload dialog is invoked by a Flash, Java, or Silverlight application, the macro may not be able to continue once the dialog appears and remains stuck. In this case, depending on how the file upload is implemented by the site / web application, you might be able to work around the issue by e.g. disabling Flash as described in this post.

See also: this post that describes a scenario where using a normal TAG command to upload the file may work by adding the CONTENT parameter to the command that clicks the Browse (or similarly named) button on the page for selecting the file.
Regards,

Tom, iMacros Support
Post Reply