watin Attachto 嵌入式浏览器
我有一个使用 watin 的 ac# windows 窗体项目。
我想附加到表单上的网络浏览器控件,这可能吗?是否有关于如何附加到嵌入式 Web 浏览器控件的示例代码。
//Looks like this only works for actual IExplorer instances.
window = WatiN.Core.Browser.AttachTo<WatiN.Core.IE>(WatiN.Core.Find.ByTitle("Google"));
I have a c# windows form project using watin.
I would love to attach to the web-browser control on a form, is this possible? Is there any sample code on how to attach to the embedded web-browser control.
//Looks like this only works for actual IExplorer instances.
window = WatiN.Core.Browser.AttachTo<WatiN.Core.IE>(WatiN.Core.Find.ByTitle("Google"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我用来附加到嵌入式 Web 浏览器控件的代码:
WatiN.Core.IE window = null;
WatiN.Core.Settings.AutoStartDialogWatcher = false;
窗口 = new WatiN.Core.IE(webBrowser1.ActiveXInstance);
This is code I used to attach to the Embeded Web Browser Control:
WatiN.Core.IE window = null;
WatiN.Core.Settings.AutoStartDialogWatcher = false;
window = new WatiN.Core.IE(webBrowser1.ActiveXInstance);
也许我在这里遗漏了一些东西,但是您不能使用
WebBrowser
控件并将其附加到 WinForms 应用程序吗?Perhaps i'm missing something here, but can you not use a
WebBrowser
control and attach it to the WinForms application?对我来说它给出了错误。
WatiN.Core.Exceptions.TimeoutException 未处理
Message="Internet Explorer 繁忙时超时"
来源=“WatiN.Core”
For me it gave the error.
WatiN.Core.Exceptions.TimeoutException was unhandled
Message="Timeout while Internet Explorer busy"
Source="WatiN.Core"
可以使嵌入式 Web 浏览器控件与 WatiN 的对话框处理机制一起使用如果您能够获取 Web 浏览器控件的窗口句柄,如果您正在编写以下代码,这应该是完全可能的包含它的表单 (1)
您需要此窗口句柄的原因正是因为 WatiN 无法通过 IWebBrowser2.HWND 属性检索它 - 这是可以预料的。 (2)
顺便说一句,即使在处理 Microsoft 的 IE 时,我也遇到过 IWebBrowser2.HWND 属性出现问题且不稳定的行为 - 因此,这是遵循下面概述的方法来解决此类 HWND 问题的又一个原因。
一旦您掌握了 Web 浏览器控件的窗口句柄,您就可以简单地创建一个代理类来包装嵌入式 Web 浏览器的 IWebBrowser2 接口(HWND 除外),从而解决该问题。把它们放在一起,你现在可以写:
希望这有帮助。哦,这是您需要的代理类:
1:即使您没有此类表单的源代码访问权限,因为 Web 浏览器控件位于您无权访问的第 3 方应用程序中,您可以编写一个小实用程序函数来扫描所有窗口,直到您偶然发现包含您想要的网络浏览器控件的第 3 方应用程序。您可以查看 ShellWindows2.CollectInternetExplorerInstances() 内的 WatiN 源代码,了解如何实现此类扫描功能。
2:http://support.microsoft.com/kb/244310
It is possible to make an embedded webbrowser control to work with WatiN's Dialog-handling mechanism if you are able to get the window handle of the webbrowser control, which should be fully possible if you are writing the code of the form that contains it (1)
The reason you need this window handle is exactly because WatiN fails to retrieve it by means of IWebBrowser2.HWND property - this is to be expected. (2)
As a side-note, I have experienced problematic and erratic behavior by IWebBrowser2.HWND property even when dealing with Microsoft's IE - so one more reason to follow the method outlined below to workaround such HWND issues.
Once you get your hands on the window handle of the webbrowser control you can simply make a proxy class that wraps around the IWebBrowser2 interface of the embedded web-browser (with the exception of HWND) so as to workaround the problem. Putting it all together you can now write:
Hope this helps. Oh, and here's the proxy class you'll need:
1: Even if you don't you have source-code access to such a form because, say, webbrowser control is in a 3rd party app that you have no access to, you can write a small utility function to scan all windows till you stumble upon the 3rd-party app that contains the webbrowser control you are after. You can have a look in WatiN's source code, inside ShellWindows2.CollectInternetExplorerInstances() to get an idea on how you can implement such a scan-function.
2: http://support.microsoft.com/kb/244310
是的,您可以使用 WebBrowser 控件,但有一些技巧。
关键点是,因为您的应用程序提供对话框(不是 IE),所以您必须将 Settings 对象属性 AutoStartDialogWatcher 设置为 false。否则,由于对话框观察器窗口句柄无效,附件将失败。
我建议使用类似的东西:
IE newbrowser = new WatiN.Core.IE(embeddedbrowser.IWebBrowser2);
...当然,IWebBrowser2 属性名称会根据您使用的库而变化。
Yes, you can use a WebBrowser control, but there are tricks.
The key point is that because your application is providing the dialogs (not IE), you must set the Settings object property AutoStartDialogWatcher to false. Otherwise, you will fail on attachment because the dialog watcher window handle is invalid.
I would suggest using something like:
IE newbrowser = new WatiN.Core.IE(embeddedbrowser.IWebBrowser2);
...of course the IWebBrowser2 property name will change depending on the library you use.