Webbrowser完成并不显示所有元素

时间:2019-05-01 16:37:02

标签: c# webbrowser-control

我有一个正在浏览网站的Web浏览器。如果网站触发了“完成”事件,我的HTMLElementCollection会显示为“ 179”。如果我单击该按钮并调用相同的方法来获取所有HTMLElements,它将显示为“ 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!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

谢谢大家,但是我再次用google搜索了,发现了一个非常好的(对我来说有效)的解决方法。 对于所有人:我希望导航到页面后,计时器(或其他东西)等待x秒,并且在计时器等待期间,页面正在加载,然后,我想单击按钮。

首先:代码不是我做的。来自https://dotnet-snippets.de/snippet/webbrowser-navigate-and-wait-seconds/15171

首先在开头声明:

  public delegate void NavigateDoneEvent();
        public static event NavigateDoneEvent Done;

        private static System.Windows.Forms.Timer wait;

您不需要将其用作静态。

之后,您需要创建此功能

 public static void Wait(WebBrowser Browser, string Url, double Seconds)
        {
            Browser.Navigate(Url);

            wait = new System.Windows.Forms.Timer();
            wait.Interval = Convert.ToInt32(Seconds * 1000);

            wait.Tick += (s, args) =>
            {
                if (Done != null) Done();
                wait.Enabled = false;
            };

            wait.Enabled = true;
        }

您可以像下面这样调用此函数:

Wait(webBrowser1, "somesite.com", 20);
            Done += afterWaitDoSomething;