WebBrowser DocumentCompleted在我的情况下是没有用的
首先,我已经修复了WebBrowser,现在我可以在Windows窗体中正确看到该页面.我关注了此链接并将其引用到IE 11.0,因此Windows窗体中的WebBrowser控件是IE11.0浏览器实例.
First of all I've repaired WebBrowser and now I see the page correctly in my Windows Form. I folowed this link and referenced it to IE 11.0 so the WebBrowser control in my windows form is a IE11.0 browser instance.
我在以下形式的构造函数中具有此名称:
I have this in the constructor of the form:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
然后是事件处理程序
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
txtLoad.Text = (Convert.ToInt32((txtLoad.Text)) + 1).ToString();
var webBrowser = sender as WebBrowser;
webBrowser.Document.GetElementById("gwt-uid-126").InvokeMember("click");
}
一切都从单击按钮开始:
It all starts with a click of a button :
void BtnTestClick(object sender, EventArgs e)
{
webBrowser1.Navigate(@"https://play.google.com/apps/publish/?dev_acc=06010154238306490792#AppListPlace");
}
单击调用不执行任何操作.我尝试了此处列出的所有方法,但没有任何尝试.为什么我可以在屏幕上看到按钮,但是无法在代码中引用它?如果尚未加载按钮,为什么会触发documentCompleted?如何获得按钮?然后点击它?
The click invoke does nothing . I tried all methods listed here and nothing. Why can I see the button on my screen but cannot reference it in code? Why is documentCompleted firing if the button didn't load already? How can I get the button? and click it?
修复:
webBrowser1.Navigate(@"https://play.google.com/apps/publish/?dev_acc=06010154238306490792#AppListPlace");
while (WebBrowser1.ReadyState != WebBrowserReadyState.Complete) {
txtLoad.Text = WebBrowser1.ReadyState.ToString();
Application.DoEvents();
System.Threading.Thread.Sleep(1);
}
webBrowser1.Document.GetElementById("gwt-uid-126").InvokeMember("click");
即使文档未完全加载,我也有对WebBrowserDocumentCompleted事件的调用.
示例:
让我们说页面源代码中有2个脚本元素:
there i a call to the WebBrowserDocumentCompleted event even if the document is not fully loaded.
example:
lets say that there are 2 script elements inside the page source code:
<script src="1.js"></script>
//Here There Is A Call To WebBrowserDocumentCompleted
<script src="2.js"></script>
//Here There Is A Call To WebBrowserDocumentCompleted
............
body
.............
</html>
//Here There Is A Call To WebBrowserDocumentCompleted
即使浏览器完成了脚本/样式表的加载,Web浏览器仍会调用此事件-页面尚未真正加载完毕.
the webbrowser is calling this event even after he done loading script/stylesheet - the page is not really fully loaded yet.