Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

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


  • Please log in to reply
9 replies to this topic

#1 JadeGolem

JadeGolem

    Seeker

  • Active Members
  • 11 posts

Posted 09 July 2010 - 04:56 AM

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







#2 Juvigy

Juvigy

    Experimental Drugs Abuser

  • Active Members
  • PipPipPipPipPipPip
  • 1,282 posts

Posted 09 July 2010 - 06:32 AM

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.

#3 JadeGolem

JadeGolem

    Seeker

  • Active Members
  • 11 posts

Posted 09 July 2010 - 08:04 AM

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.

#4 Juvigy

Juvigy

    Experimental Drugs Abuser

  • Active Members
  • PipPipPipPipPipPip
  • 1,282 posts

Posted 09 July 2010 - 08:31 AM

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?

#5 JadeGolem

JadeGolem

    Seeker

  • Active Members
  • 11 posts

Posted 09 July 2010 - 02:10 PM

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.

#6 Juvigy

Juvigy

    Experimental Drugs Abuser

  • Active Members
  • PipPipPipPipPipPip
  • 1,282 posts

Posted 09 July 2010 - 02:46 PM

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 ?

#7 DaleHohm

DaleHohm

    Think of IE as an API...

  • MVPs
  • 5,985 posts

Posted 09 July 2010 - 10:48 PM

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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

 

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

 

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y
Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble


#8 JadeGolem

JadeGolem

    Seeker

  • Active Members
  • 11 posts

Posted 10 July 2010 - 06:24 AM

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.

#9 DaleHohm

DaleHohm

    Think of IE as an API...

  • MVPs
  • 5,985 posts

Posted 08 September 2010 - 03:45 AM

@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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

 

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

 

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y
Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble


#10 StrangeQuark

StrangeQuark

    Seeker

  • New Members
  • 1 posts

Posted 10 February 2011 - 01:51 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users