【发布时间】:2011-03-08 08:32:16
【问题描述】:
我是 2 天大的 .NET C# 编码器,试图从 ProjectB - C# 控制台应用程序控制 ProjectA - WinForm。
基本上,我使用 WatiN 在 ProjectA 的 WebBrowser 控件中自动进行测试。
当我运行执行 winformWithWebBrowserTest.exe 的 ProjectB 时,显示带有 webbrowser 的 winform。但随后它无法访问form1。如何从 ProjectB 访问 webbrowser 控件???
错误:
System.Runtime.IteropServices.InvalidComObjectException
COM
object that has been separated from its underlying RCW cannot be used
ProjectA WinForm: (winformWithWebBrowserTest.exe)
namespace WindowsFormsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
}//end class
}//end namespace
Project B 控制台应用程序:(WatinConsoleExample.cs)
namespace ConsoleApplication1
{
class WatinConsoleExample
{
[STAThread]
static void Main(string[] args)
{
//run ProjectA exe
System.Diagnostics.Process Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "C:\\Users\\m-takayashiki\\Documents\\Visual Studio 2010\\Projects\\winformWithWebBrowserTest\\winformWithWebBrowserTest\\bin\\Release\\winformWithWebBrowserTest.exe";
Proc.Start();
WindowsFormsApplication1.Form1 form1 = new Form1();
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
//exception occurs below ..........
var ie = new IE(form1.webBrowser1.ActiveXInstance);
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByClass("lst")).TypeText("this is awesome!!");
ie.Button(Find.ByName("btnG")).Click();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
}
【问题讨论】:
-
您不只是直接在浏览器中运行网站,而是使用众多浏览器自动化工具之一来驱动它,例如 watin 或 selenium,是否有特殊原因?
-
我的项目是使用 Selenium 在 .NET 中启用针对 webbrowser 的自动化,但似乎 WatiN 是要走的路。
标签: c# .net winforms webbrowser-control watin