首页 > 解决方案 > 浏览器完成不显示所有元素

问题描述

我有一个正在加载网站的网络浏览器。如果网站触发“完成”事件,我的 HTMLElementCollection 会显示“179”的计数。如果我单击按钮并调用相同的方法来获取所有 HTMLElement,它会告诉我“194”的计数。问题是:如果“已完成”事件正在触发,则某些 HTMLElement 未加载,需要更长的时间,而我需要单击它的 HTMLElement 也丢失了。

用代码解释:

private void Webbrowser_DocumentComplete(object pDisp, ref object URL)
        {
                if (URL.ToString() == "testsite")
                {
                    HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
                    foreach (HtmlElement e2 in c1)
                    {
                        if (e2.GetAttribute("classname") == "btn3")
                        {
                            if (e2.InnerText == "follow")
                            {
                                e2.InvokeMember("click");
                            }
                        }
                    }
                }
        }

“c1”的计数是 179。

如果我等待 1-2 秒,然后单击具有相同代码的按钮,例如:

private void Button1_Click(object sender, EventArgs e)
        {
HtmlElementCollection c1 = webBrowser1.Document.GetElementsByTagName("div");
                    foreach (HtmlElement e2 in c1)
                    {
                        if (e2.GetAttribute("classname") == "btn3")
                        {
                            if (e2.InnerText == "follow")
                            {
                                e2.InvokeMember("click");
                            }
                        }
                    }
        }

“c1”的计数是 194。

问题是:如果页面构建完成,我怎么能等待几秒钟?我需要 194 的计数,因为有一个 HTMLElement 我想点击它!

标签: c#webbrowser-control

解决方案


您可以使用计时器来处理此问题。完成以下功能以等待几秒钟。

    public void WaitForSecond(int min)
    {
        System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        if (min == 0 || min < 0) return;
        timer1.Interval = min * 1000;
        timer1.Enabled = true;
        timer1.Start();
        timer1.Tick += (s, e) =>
        {
            timer1.Enabled = false;
            timer1.Stop();

        };
        while (timer1.Enabled)
        {
            Application.DoEvents();
        }
    }

此外,您可以通过 LINQ 减少代码

HtmlElement e2= (from HtmlElement element in 
                  webBrowser1.Document.GetElementsByTagName("div") 
                  select element)
                  .Where(x => x.GetAttribute("classname") != null 
                           && x.GetAttribute("classname") =="btn3"
                           && x.InnerText != null 
                           && x.InnerText == "follow").FirstOrDefault();

首先,检查您的 HTML 元素是否已加载,否则请等待。如果加载了 HTML 元素,则处理进一步的步骤

HtmlElement e2= null;
int cnt = 0;
            do
            {
                WaitForSecond(1);
                cnt++;

                if (cnt > 60)
                {
                   MessageBox.Show("Web page not loaded");
                    break;
                }
                e2= (from HtmlElement element in webBrowser1.Document.GetElementsByTagName("div") select element)
                .Where(x => x.GetAttribute("classname") != null 
                       && x.GetAttribute("classname") =="btn3"
                       && x.InnerText != null 
                       && x.InnerText == "follow").FirstOrDefault();    
            } while e2 == null);

推荐阅读