Jump to content

ControlSend() not working as expected?


Recommended Posts

First of all, hi guys, I've been using AutoIt on and off for various projects for a while now, never got around to registering here.. so now I have!

Moving onto the main point of the thread: ControlSend().

The script I was working on, basically logs into a website, navigates to the form page, then fills out an application form with values from an INI.

Pretty simple really, however there's one field I've never had to deal with before: an FILE input element. (<input type="file" />).

So of course, I attempt to set it with the usual _IEFormElementSetValue(), thinking it would be no different to a text field.. and receive the "securuty" error.

Check out the "help", find out why, and implement an alternative.

; Get IE's hwnd property
    $hIE = _IEPropertyGet( $oIE, "hwnd" );
    
; Put together the filename
    $filename = $folder & $name & ".pdf";

; Send the text to the filename field
    ControlSend( $hIE, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", $filename, 1 );

(PS: Excuse the ";" at the end of each line, it is just a habit that I don't want to break with over-use of AutoIt).

All good, right? Wrong.

This decides to send the text "hit and miss" style.

For example, the filename: C:\Directory\filename.pdf

First attempt may send: C:\Diretory\filename.pdf

Second attempt may send: C:\irectory\filename.pdf

Third attempt may send: C:Directory\fename.pdf

Point being, it randomly misses characters.

So I try setting the key delay slower with Opt(), however this only achieves it outputting slower, there is no change in the hit & miss results. Sleep() in between? Still nothing, as it's in ControlSend().

I tried this to see how many times it did hit and miss until it got it:

$i = 0;
    While _IEFormElementGetValue( $oFilename ) <> $filename 
        ControlSend( $hIE, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "^a{DEL}" );
        ControlSend( $hIE, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", $filename, 1 );
        $i = $i + 1;
    WEnd

    MsgBox( 0, "Result", "Took " & $i & " attempts to get it correct." );

The results were shocking -- took AutoIt 18 times to get it all in one piece, and even that number I cannot count on.

However, if I focus the control and Send() to it, it appears all in one piece, the first time.

This isn't a perfect method though, this counts on the window being visible -- which for my case is fine, however for other cases (and for this case in future) requiring it to be visible is not an option.

Is it possible I am doing something wrong here, or is this a bug with ControlSend().

Thanks,

twostars.

Edited by twostars
Link to comment
Share on other sites

From helpfile at _IEFormElementSetValue()

Note: You cannot use _IEFormElementSetValue to set the value of an INPUT TYPE=FILE element. Browser security restrictions prevent this element from being scripted. See the example below for a workaround.

; *******************************************************
; Example 5 - Set the value of an INPUT TYPE=FILE element
;               Same as previous example, but with invisible window
;               (security restrictions prevent using _IEFormElementSetValue)
; *******************************************************
;
#include <IE.au3>

$oIE = _IE_Example("form")

; Hide the browser window to demonstrate sending text to invisible window
_IEAction($oIE, "invisible")

$oForm = _IEFormGetObjByName($oIE, "ExampleForm")
$oInputFile = _IEFormElementGetObjByName($oForm, "fileExample")

; Assign input focus to the field and then send the text string
_IEAction($oInputFile, "focus")
$hIE = _IEPropertyGet($oIE, "hwnd")
ControlSend($hIE, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "C:\myfile.txt")

MsgBox(0, "Success", "Value set to C:\myfile.txt")
_IEAction($oIE, "visible")

Important is

_IEAction($oInputFile, "focus")

From your description I think something may (javascript?) steal focus from your file input type field for a small piece of time.

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...