Press save button of “File download dialog” of int

2019-04-09 07:13发布

I am working on internet explorer automation and part of it involves downloading files from a site whcih is hosted on asp 2.0 and uses forms based authentication, so to create end to end automation I used browser automation.

I was able to reach to the step where I can get to click on a URL which brings the "File Download" dialog of the browser, then I was trying to make use of SendKeys to click on the save button but to no avail it was not working.

Here is the code where I make use of FindWindow method to get the hWnd pointer of the File Download Dialog, and then using setActiveWindow I make it the active window so that the SendKeys commands works on it and then using SendKeys I tried to send Alt + S but it didn't work. I observed that, Tab, Escape and Enter works, but then Enter on Save button doesn't work.

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);

private void Form1_Load(object sender, EventArgs e)
{
    IntPtr hwnd = FindWindow(null, "File Download");
    IntPtr nullptr = (IntPtr)0;
    if (hwnd != nullptr)
    {
        SetActiveWindow(hwnd);
        SendKeys.SendWait("%S");
    }
}

Using the same code I was able to access notepad by changing the value in FindWindow to "Untitled - Notepad".

Do I need to do something different as it is a dialog and now a window? I am using IE8.

This is the alternate code I tried after the answer.

IntPtr hwnd = FindWindow(null, "File Download");
            IntPtr hokBtn = IntPtr.Zero;
            hokBtn = FindWindowEx(hwnd, hokBtn, "Button", IntPtr.Zero);
            hokBtn = FindWindowEx(hwnd, hokBtn, "Button", IntPtr.Zero);
            uint id = GetDlgCtrlID(hokBtn);
            SetActiveWindow(hwnd);
            IntPtr res = SendMessage(hokBtn, (int)0x00F5, 0, IntPtr.Zero);
            if (res.ToInt32() == 1)
                MessageBox.Show("success");

For clarity I am adding the screen of the dialog.

alt text http://www.freeimagehosting.net/uploads/4f23586401.png

7条回答
Luminary・发光体
2楼-- · 2019-04-09 07:52

None of the code suggested worked, I ended up to use AutoIt script to close a Print Dialog, the code follows:

Local $hWnd = WinWait("[CLASS:#32770]", "Print", 20)
WinActivate($hWnd)
WinWaitActive("[CLASS:#32770]", "Print", 10)
Sleep(100)
Send("{ENTER}")
查看更多
登录 后发表回答