Jump to content

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


Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 1 month later...

@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

Link to comment
Share on other sites

  • 5 months later...

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.
Link to comment
Share on other sites

  • 4 years later...

My russion Work example: (for russion site rghost, for Windows 7 RUS, IE11) :
 

#include <IE.au3>
Global $oIE = _IECreate("http://rghost.ru/")
Global $iTimer=TimerInit()
$oIE.navigate('javascript:document.getElementById("choose").click();void(0);',0)
ConsoleWrite("===time: "&TimerDiff($iTimer)&"==="&@CRLF)
;Beep(900, 1000)
Global $sFilePath=@ScriptFullPath
Global $hwnd=WinWait("Выбор выкладываемого файла","",5)
If Not $hwnd Then Exit 2
ControlSetText($hwnd,"","Edit1",$sFilePath)
Sleep(3000); задержка, чтобы было видно
ControlClick($hwnd,"","Button1")

 

Link to comment
Share on other sites

4 years too late I see, unless you have really slow e-mail?! Anyway the reason a file input cannot be automated is because it's a security vulnerability if it were.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 10 months later...

This is an old thread, but google will find it when people search about this problem, so here is my solution.

Script will hang until "Choose File to Upload" dialog is open, so you can not write any text. Solution to this problem is to create second AutoIt script, you can start and stop it from your main script, or just start it manually. You can pass the path to file via Clipboard, create some Label in GUI, write it to file etc etc.

Passing variable from main script

$oForm = _IEFormGetObjByName($oIE, "form_ID")
...
_ClipBoard_SetData("c:\example.jpg")
$oFormSelectFile = _IEFormElementGetObjByName($oForm, "open_file_element_ID")
_IEAction($oFormSelectFile, "click")

Second script

#include <Clipboard.au3>

While 1

  $handle = WinWait("Choose File to Upload")
  Sleep(300)
  $file = _ClipBoard_GetData()
  ControlSend($handle, '', 'Edit1', $file & '{ENTER}')
  Sleep(300)

WEnd

 

Edited by Maintel
Link to comment
Share on other sites

  • 5 months later...
On 30/6/2016 at 4:33 AM, Maintel said:

This is an old thread, but google will find it when people search about this problem, so here is my solution.

Script will hang until "Choose File to Upload" dialog is open, so you can not write any text. Solution to this problem is to create second AutoIt script, you can start and stop it from your main script, or just start it manually. You can pass the path to file via Clipboard, create some Label in GUI, write it to file etc etc.

Passing variable from main script

$oForm = _IEFormGetObjByName($oIE, "form_ID")
...
_ClipBoard_SetData("c:\example.jpg")
$oFormSelectFile = _IEFormElementGetObjByName($oForm, "open_file_element_ID")
_IEAction($oFormSelectFile, "click")

Second script

#include <Clipboard.au3>

While 1

  $handle = WinWait("Choose File to Upload")
  Sleep(300)
  $file = _ClipBoard_GetData()
  ControlSend($handle, '', 'Edit1', $file & '{ENTER}')
  Sleep(300)

WEnd

 

Thanks alot :)

Link to comment
Share on other sites

  • 1 year later...

The example is full. The idea is that the first script starts another script to handle the 'choose file to upload' popup that stops the execution of the first script. After the second script handles the popup, the execution of the first script continues and the second one is closed.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...