1

I'm trying to make a bot that goes on to a streaming site, and downloads stuff from there for me. The thing is that it's behaving weirdly, i'm searching for elements using a foreach loop, since the elements only have classes and not ids. The weird thing is that i need to put MessageBox.Show() before the foreach loops, otherwise it won't do anything.

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

And sometimes it will out of the blue, tell me that var elements had a null reference exception.

Jojo01
  • 1,269
  • 4
  • 14
  • 35
  • 3
    The `MessageBox.Show` is probably allowing the browser control to finish downloading the page. You should probably look to see what event fires when the downloading has finished, and hook into that instead. – James Thorpe Jul 18 '16 at 10:25
  • Yeah, do you know a callback method on the web browser? – Jojo01 Jul 18 '16 at 10:29
  • I found a way to make a void that fires everytime the browser has completed loading a page here: http://stackoverflow.com/questions/583897/c-sharp-how-to-wait-for-a-webpage-to-finish-loading-before-continuing. The only problem is it's going to fire everytime a page is loaded, so it'll need a lot of ifs. – Jojo01 Jul 18 '16 at 10:42
  • Are you intending for this to be an automated bot? Why not just use HttpClient from a console app? – Matthew Whited Jul 18 '16 at 11:05
  • @MatthewWhited i prefer to have a gui, instead of a console. – Jojo01 Jul 18 '16 at 11:10
  • Still HttpClient is designed to do work in the background. WebBrowser control is designed for human interaction. You will fight yourself less if you use HttpClient. – Matthew Whited Jul 18 '16 at 11:14
  • @MatthewWhited you're right, but it also helps with debugging if you can see the browser in action. – Jojo01 Jul 18 '16 at 11:18

1 Answers1

0

I ended up solving this like this:

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

Thanks to James Thorpe for pointing out the issue.

Jojo01
  • 1,269
  • 4
  • 14
  • 35