Key strokes in watin

余生颓废 提交于 2019-12-11 11:32:19

问题


I am trying to get my watin app to push the down key but i cant see how to do it. I have seen it in a previous post you should use :

System.Windows.Forms.SendKeys.SendWait("{DOWN}"); 

but this wont work for me as im using a web browser, any suggestions?


回答1:


You need to tell Windows to set it's focus on the Browser Window, and then send the correct key command.

For instance, this code snippet will set focus to the browser and send the "DOWN" key to the browser window.

[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr hWnd);

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

private void SendDownKey(Browser browser)
{
    SetForegroundWindow(browser.document.DomContainer.hWnd);
    SetFocus(browser.document.DomContainer.hWnd);      

    SendKeys.SendWait("{DOWN}");
}



回答2:


System.Windows.Forms.SendKeys.SendWait("{DOWN}"); suggests that you are targetting Windows App. Could you try replacing Windows with Web?

Hope it helps. Let me know if that doesnt work and I will try to dig a bit more into this.




回答3:


This is answered by several good answers in the following question:

Pass a key stroke (i.e., Enter Key) into application using WatiN scripts



来源:https://stackoverflow.com/questions/5597848/key-strokes-in-watin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!