Sign in to follow this  
Followers 0

Can't automate <input type=file ... > tag in IE

10 posts in this topic

Posted

I'm having a problem getting a "file open" box to work. On a web page that allows upload of images they use the following code in their form:

File Name: <input type=file name="Attach"><br />
Photo Name: <input type=text name="Label"><br />

With the following code I get the following error from AutoIt: "--> IE.au3 V2.4-0 Error from function _IEFormElementSetValue, $_IEStatus_InvalidObjectType (Browser securuty prevents SetValue of TYPE=FILE)"

$filename = "C:\my_path\Sample_Pictures\winter.jpeg"
$oIEMain = _IEAttach("Internet")
$oIEWork = _IEFrameGetObjByName($oIEMain, "main") ; page uses frames - this gets the right one
$oIEForm = _IEGetObjByName($oIEWork, "form1")
$oControl = _IEFormElementGetObjByName($oIEForm, "Attach")
_IEFormElementSetValue($oControl, $filename)

And with this code, I get the dialog box to pop up, but AutoIt can't interact with it (script execution pauses on _IEAction statement until the "Choose File To Upload" window closes - requires user's intervention).

$filename = "C:\my_path\Sample_Pictures\winter.jpeg"
$oIEMain = _IEAttach("Internet")
$oIEWork = _IEFrameGetObjByName($oIEMain, "main") ; page uses frames - this gets the right one
$oIEForm = _IEGetObjByName($oIEWork, "form1")
$oControl = _IEFormElementGetObjByName($oIEForm, "Attach")
_IEAction($oControl, "click")
if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then
	; Couldn't find file upload window
	ConsoleWrite("Couldn't find file upload window"&@CRLF)
	exit
EndIf
ConsoleWrite("Sending filename"&@CRLF)
Send($filename)

Does anyone have any idea how to automate populating an input tag of type=file with a filename in IE, or how to turn off the browser security that prevents the setvalue of TYPE=FILE?

Using Windows 7 and IE 8.

Thanks,

Ken

Share this post


Link to post
Share on other sites



Posted

if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then

; Couldn't find file upload window

ConsoleWrite("Couldn't find file upload window"&@CRLF)

exit

Else

Send("C:\filename.xls")

EndIf

This should work if the focus is on the input.

Share this post


Link to post
Share on other sites

Posted

if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then

; Couldn't find file upload window

ConsoleWrite("Couldn't find file upload window"&@CRLF)

exit

Else

Send("C:\filename.xls")

EndIf

This should work if the focus is on the input.

That's essentially what I've written in my second method of addressing the issue above. The problem is that the "Choose File to Upload" window does indeed have the focus (I've clicked on it while running to be sure), but the script can't run the WinWaitActive line because it's stuck on the _IEAction line that opened the window. For some reason that _IEAction call sits around waiting for the window to close before returning so I get no opportunity to interact with it.

Share this post


Link to post
Share on other sites

Posted

And if you change:

if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then
; Couldn't find file upload window
ConsoleWrite("Couldn't find file upload window"&@CRLF)
exit
Else
Send("C:\filename.xls")
EndIf

To

Sleep(2000)
Send("C:\filename.xls")

does it work?

Share this post


Link to post
Share on other sites

Posted

And if you change:

if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then
; Couldn't find file upload window
ConsoleWrite("Couldn't find file upload window"&@CRLF)
exit
Else
Send("C:\filename.xls")
EndIf

To

Sleep(2000)
Send("C:\filename.xls")

does it work?

No, it pauses on the _IEAction call so it never gets to the sleep. For instance if I run

$filename = "C:\my_path\Sample_Pictures\winter.jpeg"
$oIEMain = _IEAttach("Internet")
$oIEWork = _IEFrameGetObjByName($oIEMain, "main") ; page uses frames - this gets the right one
$oIEForm = _IEGetObjByName($oIEWork, "form1")
$oControl = _IEFormElementGetObjByName($oIEForm, "Attach")
_IEAction($oControl, "click")
sleep(1000)
ConsoleWrite("I at least got past the _IEAction click without help")
if (Not WinWaitActive("Choose File to Upload", "File &name:", 3)) Then
; Couldn't find file upload window
ConsoleWrite("Couldn't find file upload window"&@CRLF)
exit
Else
sleep(2000)
Send("C:\filename.xls")
EndIf

then the script freezes/pauses with the "Choose File to Upload" dialog open and with focus (I've left it alone for a minute or more), and the

ConsoleWrite("I at least got past the _IEAction click without help")

never gets written to the console during that minute or so, at least not until I either load a file by hand or hit cancel manually closing the dialog, at which time the "got past _IEAction" ConsoleWrite message finally gets written and the script continues and gives me the

ConsoleWrite("Couldn't find file upload window"&@CRLF)

message.

The script is clearly waiting in an infinite loop for the "Choose File to Upload" dialog to close before returning from the _IEAction call.

Share this post


Link to post
Share on other sites

Posted

I am guessing there is OK/Cancel buttons and a browse/attach button right ?

Did you try to focus the input element and just Send(filename) and after the click

the OK or browse/attach buttons ?

Share this post


Link to post
Share on other sites

Posted

Here's an example of how to automat this and get around the security restriction:

#include <IE.au3>

$oIE = _IE_Example("form")
$oT = _IEGetObjById($oIE, 'fileExample')
MouseMove(_IEPropertyGet($oT, "screenx") + _IEPropertyGet($oT, "width") - 10, _
          _IEPropertyGet($oT, "screeny") + _IEPropertyGet($oT, "height")/2)
MouseClick("left")
WinWait("Choose File to Upload")
$hChoose = WinGetHandle("Choose File to Upload")
ControlSetText($hChoose, "", "Edit1", "C:\AUTOEXEC.BAT")
ControlClick($hChoose, "", "Button2")

Dale

Share this post


Link to post
Share on other sites

Posted

Here's an example of how to automat this and get around the security restriction:

Thanks Dale - that's exactly what I needed. I had thought of a hack resembling something similar as a last resort, but this cleaner.

Share this post


Link to post
Share on other sites

Posted

@wakillon - this reply belongs on another thread. Please add it there and blank out this one. When you do I will reply with some helpful information.

Dale

Share this post


Link to post
Share on other sites

Posted

Here's an example of how to automat this and get around the security restriction:

#include <IE.au3>

$oIE = _IE_Example("form")
$oT = _IEGetObjById($oIE, 'fileExample')
MouseMove(_IEPropertyGet($oT, "screenx") + _IEPropertyGet($oT, "width") - 10, _
          _IEPropertyGet($oT, "screeny") + _IEPropertyGet($oT, "height")/2)
MouseClick("left")
WinWait("Choose File to Upload")
$hChoose = WinGetHandle("Choose File to Upload")
ControlSetText($hChoose, "", "Edit1", "C:\AUTOEXEC.BAT")
ControlClick($hChoose, "", "Button2")

Dale

Can You tell me why $oT.click instead of MouseMove/MouseClick doesn't work ? Selection File window shows,but script is not interacting with it.

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