Jump to content

Help with translating in Autoit


Recommended Posts

Microsoft has some security protections to prevent direct access to an input field of type file. The most common solution Ive come across is to either just click the browse button, which fails if you are trying to automatically fill something in, or to use SendKeys, which fails if the user doesnt have focus on the application at the moment. Taking a route similar to SendKeys, we can hook in at a lower level, and instead of simulating key presses to the current application, we can send the key presses directly to the WebBrowser document. This solves the problem of the application having to be focused.

1. public class ExtendedWebBrowser : WebBrowser  
   2. {  
   3.    const int WM_KEYDOWN = 0x100;  
   4.    const int WM_KEYUP = 0x101;  
   5.    const int WM_CHAR = 0x102;  
   6.   
   7.    [DllImpor[code]t("user32.dll")]  
   8.    static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);  
   9.   
  10.    [DllImport("user32.dll")]  
  11.    static extern byte VkKeyScan(char ch);  
  12.   
  13.    [DllImport("user32.dll")]  
  14.    static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);  
  15.   
  16.    /// <summary>  
  17.    /// Fills in an input type="file" form field.  
  18.    /// </summary>  
  19.    /// <param name="fileElement">The html file input element.</param>  
  20.    /// <param name="file">The path to the file to be uploaded.</param>  
  21.    public void FillFileInput(HtmlElement fileElement, string file)  
  22.    {  
  23.        uint child = 5;  
  24.        //Obtain the handle to the WebBrowser's document.  
  25.        IntPtr documentHandle = GetWindow(GetWindow(GetWindow(Handle, child), child), child);  
  26.   
  27.        fileElement.Focus();  
  28.        foreach (char c in file)  
  29.        {  
  30.            PostMessage(documentHandle, WM_KEYDOWN, VkKeyScan(c), 0);  
  31.            PostMessage(documentHandle, WM_CHAR, (int)c, 0);  
  32.            PostMessage(documentHandle, WM_KEYUP, VkKeyScan(c), 0);  
  33.        }  
  34.        Application.DoEvents();  
  35.    }  
  36. }
Link to comment
Share on other sites

  • Moderators

What part do you need help with?

Edit:

Aww hell, even the citation wasn't your own :)

http://www.google.com/search?q=Microsoft+h...lient=firefox-a

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

"C:\Program Files\AutoIt3\autoit.chm" --> _IEFormElementSetValue() examples:

; *******************************************************
; Example 4 - Set the value of an INPUT TYPE=FILE element
;               (security restrictions prevent using _IEFormElementSetValue)
; *******************************************************
;
#include <IE.au3>

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

; Assign input focus to the field and then send the text string
_IEAction($oInputFile, "focus")
Send("C:\myfile.txt")

; *******************************************************
; 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")
Link to comment
Share on other sites

"C:\Program Files\AutoIt3\autoit.chm" --> _IEFormElementSetValue() examples:

; *******************************************************
; Example 4 - Set the value of an INPUT TYPE=FILE element
;               (security restrictions prevent using _IEFormElementSetValue)
; *******************************************************
;
#include <IE.au3>

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

; Assign input focus to the field and then send the text string
_IEAction($oInputFile, "focus")
Send("C:\myfile.txt")

; *******************************************************
; 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")
Thanks Zedna,

I use this example in IE6 and IE7 and tha's works perfectly.

Since Internet Explorer 8 and Firefox3 it's not possible to assign a value to an input file because that's fields is readonly and it is not possible to give a value. The only way is to click on the button submit and to give manually a valu in the #32770 box. This box is a modal box and this box blocks autoit program so that we can do anaything execpt clicking on Close button. by these way, tha's while i try to do by another way, but it seems that's everyone has the same problem

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