0

I need help either fixing this error or possible getting the "Start Viewer" button clicked a different way.

private void button1_Click(object sender, EventArgs e)
{
    {
        webBrowser1.Navigate(new Uri(textBox1.Text));
        HtmlDocument doc = webBrowser1.Document;

        HtmlElementCollection classButton = webBrowser1.Document.All;
        foreach (HtmlElement element in classButton)
        {
            if (element.GetAttribute("className") == "titre_12")
            {
                element.InvokeMember("click");
            }
        }
    }
    {
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000; // 1 second
        timer1.Start();
        label2.Text = counter.ToString();
    }
}

I am getting "A first chance exception of type 'System.NullReferenceException' occurred in VPS Web Bot.exe"

For the line...

HtmlElementCollection classButton = webBrowser1.Document.All;

I am trying to simply have my bot click to start a viewer but the HTML does not have an ID so I am trying to get my element by class.

Screen shot of HTML

I am trying to have it click "Start Viewer" on https://www.websyndic.com/wv3/?qs=MTAzMDE5Nw==

I am self taught when it comes to programming so I don't always understand the proper terms. I apologize in advance.

My post was marked as a duplicate but the post it was referred to is something I already tried and it did not work.

Svek
  • 12,350
  • 6
  • 38
  • 69
Jay
  • 3
  • 2
  • Not a duplicate, post it was matched to does not solve my problem. – Jay Jun 03 '17 at 05:18
  • Yes, it is a duplicate. The solution to a null reference exception is *always* to figure out what is null. If you can determine what is null and you don't understand *why* it is null, then you have a different question that may not be a duplicate. – BJ Myers Jun 03 '17 at 05:27

1 Answers1

0

Solution

use WebBrowser.DocumentCompleted

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.DocumentCompleted += (se, ev) => 
    {

        // the rest of your code

    };
    
    webBrowser1.Navigate("www.domain.com");
}

Extra notes

When you enclose your statments in {}

{
    webBrowser1.Navigate(new Uri(textBox1.Text));
}
{
    HtmlDocument doc = webBrowser1.Document;
    HtmlElementCollection classButton = webBrowser1.Document.All;
    ...
}

you are isolating the top from the bottom... I'm sure you meant this instead...

{
    webBrowser1.Navigate(new Uri(textBox1.Text));
    HtmlDocument doc = webBrowser1.Document;
    HtmlElementCollection classButton = webBrowser1.Document.All;
    ...
}
Community
  • 1
  • 1
Svek
  • 12,350
  • 6
  • 38
  • 69
  • The extra braces would only change the scope of objects declared inside of them. It won't have any effect on objects that are declared outside the braces. It's definitely not something one normally does, but it's not causing the problem. – BJ Myers Jun 03 '17 at 05:03
  • @BJMyers - You're right. I'll work on a fix now. – Svek Jun 03 '17 at 05:05
  • @BJMyers you marked it as a duplicate and switching "webBrowser.Navigate" to "webBrowser.DocumentCompleted" does not fix my issue. Also I am adding multiple actions under one button I need to browser to navigate to that link and then perform the action of clicking the "Start Viewer" but it does not have an ID so I had to get my element by class instead & I get the error "A first chance exception of type 'System.NullReferenceException' occurred in VPS Web Bot.exe" for the line "HtmlElementCollection classButton = webBrowser1.Document.All;" – Jay Jun 03 '17 at 05:16
  • @BJMyers making that change I get "Error 1 The event 'System.Windows.Forms.WebBrowser.DocumentCompleted' can only appear on the left hand side of += or -=" – Jay Jun 03 '17 at 05:17
  • @Jay did you copy it completely? – Svek Jun 03 '17 at 05:19
  • @Svek "Error 1 Delegate "System.Windows.Forms.WebBrowserDocumentCompletedEventHandler' does not take 0 arguments" – Jay Jun 03 '17 at 05:24
  • @Jay - Did it solve your problem? – Svek Jun 03 '17 at 05:36
  • @Svek "Error 1 A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else" – Jay Jun 03 '17 at 05:53