本文介绍了在WebBrowser中使用InvokeMember的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试在WebBrowser组件中使用InvokeMember时,我无法访问网页的更新内容.

我的代码加载了一个页面,然后找到并调用了一个按钮.

内容似乎显示在表单上,​​但是当我尝试引用Document属性时,在代码中我似乎获得了第一页数据.

下面的代码片段试图在按下网页上的按钮后保存页面数据.该文件最终包含最初加载的页面,而不是按下按钮后的页面.

希望有人可以帮助我指出正确的方向.


代码:

private void button1_Click(对象发送者,EventArgs e)
{
    //加载首页
    webBrowser1.Navigate(urlToLoad);
   而(webBrowser1.ReadyState!= WebBrowserReadyState.Complete)
    {
    Thread.Sleep(100);
    Application.DoEvents();
    }

    //查找并调用按钮
       foreach(HtmlCol中的HtmlElement HtmlEl)
       {
          字符串ElName = HtmlEl.GetAttribute("Name").ToString();
                            如果(ElName ==" ctl00 $ ContentPlaceHolder $ Find $ btnSearch")
                             {
                    HtmlEl.InvokeMember("click");
                   休息;
                             }  
    }
         
    //等待页面准备就绪
      而(webBrowser1.ReadyState!= WebBrowserReadyState.Complete)
       {
           Thread.Sleep(100);
                             Application.DoEvents();
       }

    //将html数据读入文本文件
   字符串s = webBrowser1.DocumentText;
       StreamWriter outfile =新的StreamWriter("File.txt");
    outfile.Write(s);
    outfile.Close();
}

When attempting to use InvokeMember in WebBrowser component I am having trouble accessing the updated contents of the webpage.

My code load a page then finds and invokes a button.

The content appears to show on the form, but in code when I attempt to reference the Document property I appear to get the first pages data.

Code snipet below is trying to save page data after a button on the web page is pressed. The file ends up copntaining the initially loaded page and not the one after the button press.

Hoping someone could help point me in the right direction.


Code:

private void button1_Click(object sender, EventArgs e)
{
    //Load first page
    webBrowser1.Navigate(urlToLoad);
    while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
    {
    Thread.Sleep(100);
    Application.DoEvents();
    }

    //Find and invoke button
        foreach (HtmlElement HtmlEl in HtmlCol)
        {
            string ElName = HtmlEl.GetAttribute("Name").ToString();
                if (ElName == "ctl00$ContentPlaceHolder$Find$btnSearch")
                {
                    HtmlEl.InvokeMember("click");
                    break;
                }  
    }
          
    //Wait for page to be ready
        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
        {
            Thread.Sleep(100);
                Application.DoEvents();
        }

    //Reading html data into text file
    string s = webBrowser1.DocumentText;
        StreamWriter outfile = new StreamWriter("File.txt");
    outfile.Write(s);
    outfile.Close();
}

 

 

推荐答案

 

1.尽管您可以使用"ReadyState"属性来确定WebBrowser控件是否已完全完成文档的加载,使用DocumentCompleted事件实际上更好.当WebBrowser控件触发此事件时,将确保该事件 该文档确实已经完全加载.

1. Although you can use the "ReadyState" property to determine whether the WebBrowser control has completely finished loading a document, using the DocumentCompleted event is actually better. When the WebBrowser control fires this event, it is guarenteed that the document has indeed been completely loaded.

 

2.在Navigate()方法调用之前,将事件关联到C#表单,并在文档完全完成加载后,将调用事件处理程序.

2. Wire the event to your C# form before the Navigate() method invokation and when the document has completely finished loading, the event handler will be invoked.

 

3.在您的特定情况下,您需要提供2个事件处理程序:一个用于检测第一个URL的文档何时被加载,另一个用于检测第二个URL的文档何时被加载的事件处理程序.这是一些示例代码:

3. In your particular case, you need to provide 2 event handlers : one to detect when the first URL's document has been loaded and one to detect when the second URL's document has been loaded. Here are some example codes :

3.1以下内容将WebBrowser控件的DocumentCompleted事件连接到"SearchForNameButton".方法.这是在我们导航到第一个URL之前完成的:

3.1 The following wires the WebBrowser control's DocumentCompleted event to the "SearchForNameButton" method. This is done just before we navigate to the first URL :

       private void button1_Click(对象发送者,EventArgs e)
       {
           webBrowser1.DocumentCompleted + =
                            新的WebBrowserDocumentCompletedEventHandler(SearchForNameButton);

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.DocumentCompleted +=
                new WebBrowserDocumentCompletedEventHandler(SearchForNameButton);

           //加载首页
           webBrowser1.Navigate(urlToLoad);
       }

            //Load first page
            webBrowser1.Navigate(urlToLoad);
        }

3.2当为完全加载第一页而触发DocumentCompleted事件时,我们立即删除SearchForNameButton事件处理程序,并继续在文档中搜索名称".按钮单击.单击此之后,我们连接WebBrowser 控件的DocumentCompleted事件传递给另一个事件处理程序:方法

3.2 When the DocumentCompleted event is fired for the complete loading of the first page, we immediately remove the SearchForNameButton event handler and proceed to search the document for the "Name" button to click. After this is clicked, we wire the WebBrowser control's DocumentCompleted event to another event handler : "SaveSecondDocument" method

      私有void SearchForNameButton(对象发送者,WebBrowserDocumentCompletedEventArgs e)
       {
           webBrowser1.DocumentCompleted-= SearchForNameButton;

        private void SearchForNameButton(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.DocumentCompleted -= SearchForNameButton;

           foreach(HtmlCol中的HtmlElement HtmlEl)
           {
                            字符串ElName = HtmlEl.GetAttribute("Name").ToString();
                             if(ElName ==" ctl00

            foreach (HtmlElement HtmlEl in HtmlCol)
            {
                string ElName = HtmlEl.GetAttribute("Name").ToString();
                if (ElName == "ctl00




这篇关于在WebBrowser中使用InvokeMember的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:46