网页浏览器的行为问题 [英] Webbrowser behaviour issues
问题描述
我想要的网页浏览器使用.NET C#自动化。问题是,控制或者我应该说的IE浏览器在不同计算机上的行为很奇怪。例如,我在clickin链接,fillup这样一号计算机上的Ajax弹出的形式,没有任何错误:
I am trying to automate Webbrowser with .NET C#. The issue is that the control or should I say IE browser behaves strange on different computers. For example, I am clickin on link and fillup a Ajax popup form on 1st computer like this, without any error:
private void btn_Start_Click(object sender, RoutedEventArgs e)
{
webbrowserIE.Navigate("http://www.test.com/");
webbrowserIE.DocumentCompleted += fillup_LoadCompleted;
}
void fillup_LoadCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
System.Windows.Forms.HtmlElement ele = web_BrowserIE.Document.GetElementById("login");
if (ele != null)
ele.InvokeMember("Click");
if (this.web_BrowserIE.ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
{
web_BrowserIE.Document.GetElementById("login").SetAttribute("value", myUserName);
web_BrowserIE.Document.GetElementById("password").SetAttribute("value", myPassword);
foreach (System.Windows.Forms.HtmlElement el in web_BrowserIE.Document.GetElementsByTagName("button"))
{
if (el.InnerText == "Login")
{
el.InvokeMember("click");
}
}
web_BrowserIE.DocumentCompleted -= fillup_LoadCompleted;
}
}
不过,上述code不会对第二个PC和点击的唯一方式工作是这样的:
However, the above code wont work on 2nd pc and the only way to click is like this:
private void btn_Start_Click(object sender, RoutedEventArgs e)
{
webbrowserIE.DocumentCompleted += click_LoadCompleted;
webbrowserIE.Navigate("http://www.test.com/");
}
void click_LoadCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (this.webbrowserIE.ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.HtmlElement ele = webbrowserIE.Document.GetElementById("login");
if (ele != null)
ele.InvokeMember("Click");
webbrowserIE.DocumentCompleted -= click_LoadCompleted;
webbrowserIE.DocumentCompleted += fillup_LoadCompleted;
}
}
void click_LoadCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
webbrowserIE.Document.GetElementById("login_login").SetAttribute("value", myUserName);
webbrowserIE.Document.GetElementById("login_password").SetAttribute("value", myPassword);
//If you know the ID of the form you would like to submit:
foreach (System.Windows.Forms.HtmlElement el in webbrowserIE.Document.GetElementsByTagName("button"))
{
if (el.InnerText == "Login")
{
el.InvokeMember("click");
}
}
webbrowserIE.DocumentCompleted -= click_LoadCompleted;
}
所以,在第二个解决方案我要调用两个加载完成,链。可能有人提出建议我应该怎么可以处理这个问题呢?此外,为了更稳健的做法的建议将是非常有益的。谢谢你在前进
So, in second solution I have to call two Load Completed Chains. Could someone advise on how should I can handle this issue? Also, a proposal for more robust approach would be very helpfull. Thank you in advance
推荐答案
我可以推荐两件事情:
- 请不要在
的DocumentComplete
事件执行你的code,而一旦做DOM的在window.onload 事件。 - 要确保你的网页在
web浏览器
控制的方式,因为它会在完整的Internet Explorer浏览器一样,考虑实施<一个行为href=\"http://stackoverflow.com/questions/18333459/c-sharp-webbrowser-ajax-call/18333982#18333982\">Feature控制。
- Don't execute your code upon
DocumentComplete
event, rather do upon DOM window.onload event. - To make sure your web page behaves in
WebBrowser
control the same way as it would in full Internet Explorer browser, consider implementing Feature Control.
有一个建议,根据您的code的结构。显然,你执行一系列导航/句柄的DocumentComplete
行动。这可能是更加自然和方便使用异步/的await
这一点。这里是这样,有或没有异步/的await
的一个例子。它说明了如何处理的onload
,也:
There's one more suggestion, based on the structure of your code. Apparently, you perform a series of navigation/handle DocumentComplete
actions. It might be more natural and easy to use async/await
for this. Here's an example of doing this, with or without async/await
. It illustrates how to handle onload
, too:
async Task DoNavigationAsync()
{
bool documentComplete = false;
TaskCompletionSource<bool> onloadTcs = null;
WebBrowserDocumentCompletedEventHandler handler = delegate
{
if (documentComplete)
return; // attach to onload only once per each Document
documentComplete = true;
// now subscribe to DOM onload event
this.wb.Document.Window.AttachEventHandler("onload", delegate
{
// each navigation has its own TaskCompletionSource
if (onloadTcs.Task.IsCompleted)
return; // this should not be happening
// signal the completion of the page loading
onloadTcs.SetResult(true);
});
};
// register DocumentCompleted handler
this.wb.DocumentCompleted += handler;
// Navigate to http://www.example.com?i=1
documentComplete = false;
onloadTcs = new TaskCompletionSource<bool>();
this.wb.Navigate("http://www.example.com?i=1");
await onloadTcs.Task;
// the document has been fully loaded, you can access DOM here
MessageBox.Show(this.wb.Document.Url.ToString());
// Navigate to http://example.com?i=2
// could do the click() simulation instead
documentComplete = false;
onloadTcs = new TaskCompletionSource<bool>(); // new task for new navigation
this.wb.Navigate("http://example.com?i=2");
await onloadTcs.Task;
// the document has been fully loaded, you can access DOM here
MessageBox.Show(this.wb.Document.Url.ToString());
// no more navigation, de-register DocumentCompleted handler
this.wb.DocumentCompleted -= handler;
}
下面是同样的code的没有异步/的await
的模式(用于.NET 4.0):
Here's the same code without async/await
pattern (for .NET 4.0):
Task DoNavigationAsync()
{
// save the correct continuation context for Task.ContinueWith
var continueContext = TaskScheduler.FromCurrentSynchronizationContext();
bool documentComplete = false;
TaskCompletionSource<bool> onloadTcs = null;
WebBrowserDocumentCompletedEventHandler handler = delegate
{
if (documentComplete)
return; // attach to onload only once per each Document
documentComplete = true;
// now subscribe to DOM onload event
this.wb.Document.Window.AttachEventHandler("onload", delegate
{
// each navigation has its own TaskCompletionSource
if (onloadTcs.Task.IsCompleted)
return; // this should not be happening
// signal the completion of the page loading
onloadTcs.SetResult(true);
});
};
// register DocumentCompleted handler
this.wb.DocumentCompleted += handler;
// Navigate to http://www.example.com?i=1
documentComplete = false;
onloadTcs = new TaskCompletionSource<bool>();
this.wb.Navigate("http://www.example.com?i=1");
return onloadTcs.Task.ContinueWith(delegate
{
// the document has been fully loaded, you can access DOM here
MessageBox.Show(this.wb.Document.Url.ToString());
// Navigate to http://example.com?i=2
// could do the 'click()' simulation instead
documentComplete = false;
onloadTcs = new TaskCompletionSource<bool>(); // new task for new navigation
this.wb.Navigate("http://example.com?i=2");
onloadTcs.Task.ContinueWith(delegate
{
// the document has been fully loaded, you can access DOM here
MessageBox.Show(this.wb.Document.Url.ToString());
// no more navigation, de-register DocumentCompleted handler
this.wb.DocumentCompleted -= handler;
}, continueContext);
}, continueContext);
}
请注意,这两种情况下,它仍是一块异步code的它返回的任务对象。这里有一个如何处理这类任务的完成一个例子:
Note, it both cases it is still a piece of asynchronous code which returns a Task object. Here's an example of how to handle the completion of such task:
private void Form1_Load(object sender, EventArgs e)
{
DoNavigationAsync().ContinueWith(_ => {
MessageBox.Show("Navigation complete!");
}, TaskScheduler.FromCurrentSynchronizationContext());
}
使用抽头模式的好处这里是 DoNavigationAsync
是一个自包含的,独立的方法。它可以重复使用,它不会与父对象的状态干扰(在这种情况下,主要的形式)。
The benefit of using TAP pattern here is that DoNavigationAsync
is a self-contained, independent method. It can be reused and it doesn't interfere with the state of parent object (in this case, the main form).
这篇关于网页浏览器的行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!