1
相关:How to use watin with WebBrowser control?华廷与.NET WebBrowser控件
所以,我读了相应的部分,其中讨论了STA模型在这里:
http://watin.org/documentation/sta-apartmentstate/
现在的问题是,你如何处理这个?
我创建了一个方法来导航:
driver.NavigateTo(webBrowser1.ActiveXInstance, "http://www.google.com");
现在我尝试登录到目标站点:
public void OpenSiteAndLogin(object browser)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Open Site and Login");
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
//browserInstance = new IE(browser);
//browserInstance.GoTo(baseURL);
using (var browserInstance = new IE(browser))
{
try
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Looking for Account Element");
TextField account = browserInstance.TextField(Find.ByClass("account-link"));
if (_Log.IsDebugEnabled) _Log.DebugFormat("Logged In");
loggedIn = true;
}
catch (Exception)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Not found, not logged in");
}
if (!loggedIn)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("not logged in, click login");
Element login = browserInstance.Element(Find.ByText("Login"));
login.Click();
if (_Log.IsDebugEnabled) _Log.DebugFormat("fill in login info");
browserInstance.TextField(Find.ById("id_auth-username")).TypeText("test");
browserInstance.TextField(Find.ById("id_auth-password")).TypeText("test");
browserInstance.Element(Find.BySelector("input[type=submit]")).Click();
if (_Log.IsDebugEnabled) _Log.DebugFormat("submit login info");
if (_Log.IsDebugEnabled) _Log.DebugFormat("wait for account element");
browserInstance.WaitForComplete();
if (_Log.IsDebugEnabled) _Log.DebugFormat("Logged In");
loggedIn = true;
}
}
}
);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
public void NavigateTo(object browser, string url)
{
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
browserInstance = new IE(browser);
browserInstance.GoTo(url);
} );
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
我在我的浏览器传递的ActiveX实例调用此
所以,我尝试点击一个按钮来做到这一点:
WebDriver driver = new WebDriver();
driver.NavigateTo webBrowser1.ActiveXInstance, "http://www.google.com");
driver.OpenSiteAndLogin(webBrowser1.ActiveXInstance);
我想是这样的:
WebDriver driver = new WebDriver();
driver.NavigateTo(webBrowser1.ActiveXInstance, "http://www.google.com");
Application.DoEvents();
Thread.Sleep(5000);
driver.OpenSiteAndLogin(webBrowser1.ActiveXInstance);
但这并不工作要么,整个事情挂起。什么是等待这个衍生线程的最佳方式,而不锁定UI线程,并在完成后转到下一个操作?
做您尝试单击调试器中的“暂停”按钮,知道它在危在旦夕?你的日志消息说什么? –
关于它的任何解决方案? – Kiquenet
你做了什么让browserInstance = new IE(浏览器)不返回null?如果我在表单类中尝试这种方法,那么它就可以工作。但是,如果我尝试将WebBrowser实例发送到另一个类并让它在那里实例化,它将失败。 – dylanmensaert