c#browser bot问题

时间:2016-07-18 10:20:36

标签: c# web bots

我试图制作一个流媒体网站的机器人,并从那里下载东西给我。问题在于它表现得很奇怪,我正在使用foreach循环搜索元素,因为元素只有类而不是id。奇怪的是我需要在foreach循环之前放置MessageBox.Show(),否则它不会做任何事情。

代码(C#):

private void startDownload()
{
            infoLabel5.Text = "Download started on series: " + series;
            infoLabel5.ForeColor = Color.Black;
            browser.ScriptErrorsSuppressed = true;
            browser.Navigate("http://www.anilinkz.tv");
            browserProgress.Increment(10);
            var elements = browser.Document.GetElementsByTagName("input");
            MessageBox.Show("set value");
            foreach (HtmlElement element in elements)
            {
                if (element.GetAttribute("classname") == "query")
                {
                    element.SetAttribute("value", series);
                    downloadStep2();
                }
            }
}
private void downloadStep2()
{
            infoLabel5.Text = "Download started on series: " + series;
            infoLabel5.ForeColor = Color.Black;
            browserProgress.Increment(5);
            var elements = browser.Document.GetElementsByTagName("input");
            MessageBox.Show("Click");
            foreach (HtmlElement element in elements)
            {
                if (element.GetAttribute("classname") == "searchbtn")
                {
                    element.InvokeMember("click");
                }
            }
}

有时它会突然出现,告诉我var元素有一个空引用异常。

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题:

private void startDownload()
{
            action = "step1";
            infoLabel5.Text = "Download started on series: " + series;
            infoLabel5.ForeColor = Color.Black;
            browser.ScriptErrorsSuppressed = true;
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browserCompleted);
            browser.Navigate("http://www.anilinkz.tv");
            browserProgress.Increment(10);
}
private void browserCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
            if (action == "step1")
            {
                downloadStep1();
            }
}
private void downloadStep1()
{
            action = null;
            infoLabel5.Text = "Download started on series: " + series;
            infoLabel5.ForeColor = Color.Black;
            browserProgress.Increment(5);
            var elements = browser.Document.GetElementsByTagName("input");
            foreach (HtmlElement element in elements)
            {
                if (element.GetAttribute("classname") == "query")
                {
                    element.SetAttribute("value", series);
                    downloadStep2();
                }
            }
}
private void downloadStep2()
{
            infoLabel5.Text = "Download started on series: " + series;
            infoLabel5.ForeColor = Color.Black;
            browserProgress.Increment(5);
            var elements = browser.Document.GetElementsByTagName("input");
            foreach (HtmlElement element in elements)
            {
                if (element.GetAttribute("classname") == "searchbtn")
                {
                    element.InvokeMember("click");
                }
            }
}

感谢James Thorpe指出了这个问题。