Ani*_*oel 5 c# internet-explorer download sendkeys
我正在研究Internet Explorer自动化,其中一部分涉及从asp 2.0上托管的站点下载文件并使用基于表单的身份验证,因此为了创建端到端自动化,我使用了浏览器自动化.
我能够到达我可以点击一个带有浏览器"文件下载"对话框的URL的步骤,然后我试图利用SendKeys点击保存按钮,但无济于事不工作
下面是我使用FindWindow方法获取文件下载对话框的hWnd指针的代码,然后使用setActiveWindow使其成为活动窗口,以便SendKeys命令对其起作用,然后使用SendKeys我尝试发送Alt +但它没有用.我观察到,Tab,Escape和Enter工作,但是Enter on Save按钮不起作用.
[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");
}
}
Run Code Online (Sandbox Code Playgroud)
使用相同的代码我可以通过将FindWindow中的值更改为"Untitled - Notepad"来访问记事本.
我是否需要做一些不同的事情,因为它是一个对话框,现在是一个窗口?我正在使用IE8.
这是我在答案后尝试的备用代码.
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");
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我正在添加对话框的屏幕.
alt text http://www.freeimagehosting.net/uploads/4f23586401.png
小智 1
好吧,你必须找到带有下载对话框标题的窗口。然后您必须找到标题为下载按钮/的窗口,然后发送到该窗口单击消息
BM_CLICK = 0x00F5
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern uint GetDlgCtrlID(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
//hDialog - handle of dialog window. idBtn - Id of button
public static bool ClickButtonOnDialog(IntPtr hDialog, UInt32 idBtn)
{
IntPtr res = IntPtr.Zero;
uint id;
IntPtr hOkBtn = IntPtr.Zero;
int attempt = 0;
do
{
Thread.Sleep(300);
//searching for button
hOkBtn = User32.FindWindowEx(hDialog, hOkBtn, "Button", IntPtr.Zero);
id = User32.GetDlgCtrlID(hOkBtn);
attempt++;
} while (id != idBtn && attempt < 20);
if (!hOkBtn.Equals(IntPtr.Zero))
{
//click the button
res = User32.SendMessage(hOkBtn, (int)WindowsMessages.BM_CLICK, 1, IntPtr.Zero);
}
if (res.ToInt32() == 1)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 winspector (spy++ 的类似物)。这是非常有用的实用程序。你可以发现很多关于windows的东西;)
| 归档时间: |
|
| 查看次数: |
15850 次 |
| 最近记录: |