WebBrowser DocumentCompleted在我的情况下是无用的[英] WebBrowser DocumentCompleted is useless in my case

本文是小编为大家收集整理的关于WebBrowser DocumentCompleted在我的情况下是无用的的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

首先,我修复了WebBrowser,现在我以Windows表单正确看到页面.我在这个链接并将其引用到IE 11.0,因此Windows form中的WebBrowser控件是IE11.0浏览器实例.

我在形式的构造函数中具有:

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");
    }

这一切都以单击按钮开始:

void BtnTestClick(object sender, EventArgs e)
    {
        webBrowser1.Navigate(@"https://play.google.com/apps/publish/?dev_acc=06010154238306490792#AppListPlace");
    }

点击命令什么都不做.我尝试了所有列出的方法 neshter 什么都没有.为什么我可以在屏幕上看到按钮,但不能在代码中引用它?如果该按钮尚未加载,为什么要启动documentComplete.如何获取按钮?并单击它?

推荐答案

修复:

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");


即使文档未满足 示例:
可以说,页面源代码中有2个脚本元素:

<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

WebBrowser即使完成加载脚本/样式表之后,WebBrowser也在调用此事件 - 页面尚未真正加载.

本文地址:https://www.itbaoku.cn/post/2252177.html

问题描述

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);

then the event handler

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");
    }

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?

推荐答案

Fix:

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");


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

the webbrowser is calling this event even after he done loading script/stylesheet - the page is not really fully loaded yet.