WinForms Web 浏览器控件强制重新聚焦?
我正在尝试自动化一个需要重复单击按钮的网络流程。当我的代码“单击”该按钮(从表单上的 WebBrowser 控件获取的 HtmlElement)时,它将焦点带回我的应用程序,更具体地说是 WebBrowser 控件。我希望更好地自动化此过程,以便用户可以在该过程进行时执行其他操作,但如果窗口由于获得焦点而未最小化自身,则不会发生这种情况。
与点击相关的代码是:
HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");
我也尝试过 Button.RaiseEvent("onclick") 并得到完全相同的结果,但存在焦点问题等。
我也尝试过隐藏表单,但是当调用 InvokeMember/RaiseEvent 方法时,我正在处理的任何内容都会失去焦点,但由于表单不可见,因此焦点似乎无处可去。
关于网络浏览器的唯一非默认的事情是它的 URI 设置为我的页面,并且 ScriptErrorsSuppressed 设置为 True。
I'm trying to automate a web process where I need to click a button repeatedly. When my code "clicks" that button (an HtmlElement obtained from the WebBrowser control I have on my form) then it brings focus back to my application, more specifically the WebBrowser control. I wish to better automate this process so that the user can do other things while the process is going on, but that can't happen if the window is unminimizing itself because it's attaining focus.
The code associated with the clicking is:
HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");
I've also tried button.RaiseEvent("onclick") and am getting the exact same results, with focus problems and all.
I've also tried hiding the form, but when the InvokeMember/RaiseEvent method is called, whatever I was working on loses focus but since the form is not visible then the focus seems to go nowhere.
The only non-default thing about the webbrowser is it's URI being set to my page and ScriptErrorsSuppressed being set to True.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要点击这个按钮?它正在发送某种表格吗?
如果是,您可以使用
WebClient
并在没有图形界面的情况下模拟发送表单。基本上,几乎任何重要的事情都需要使用
Get
Post
或multipart/post
连接到网站,因此WebClient
将是完美的。您可以简单地获取此按钮所在的网站并解析它以了解单击它会执行什么操作。然后模拟自己的动作。Why do You need to click this button? It's sending some form?
If yes, you can use
WebClient
and simulate sending form without graphic interface.Basicly almost anything significant requires connection to website using
Get
Post
ormultipart/post
soWebClient
will be perfect. You can simply get the site wich is this button on and parse it to get what clicking it do. And then simulate your own action.在 IE7 或更高版本中,您可以在网络浏览器站点上实施 IProtectFocus 并拒绝焦点更改。如果您使用原始 ActiveX 或其支持此类自定义的包装器,情况会好得多,例如 csexwb。如果必须使用winform webbrowser控件,则需要创建自己的webbrowser站点。
In IE7 or higher you can implement IProtectFocus on the webbrowser site and deny the focus change. You would be much better off if you use the raw ActiveX or its wrappers that support this kind of customization, e.g. csexwb. If you have to use the winform webbrowser control, you need to create your own webbrowser site.