I am working on automation of Internet researchers, and part of it is related to downloading files from a site hosting asp 2.0, and uses forms-based authentication, so I used browser automation to create end-to-end automation.
I was able to get to the point where I can click on the URL that the File Download dialog box displays in the browser, then I tried to use SendKeys to click the Save button, but it didn’t work without it.
Here is the code in which I use the FindWindow method to get the hWnd pointer of the file download dialog, and then using setActiveWindow I make it an active window so that SendKeys commands work on it, and then using SendKeys I tried to send Alt + S, but this did not work. I noticed that Tab, Escape and Enter work, but the Enter on Save button does not 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 the notepad by changing the value in FindWindow to "Untitled - Notepad".
Do I need to do something else, since this 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 a dialogue screen.
alt text http://www.freeimagehosting.net/uploads/4f23586401.png