0

I am currently working on a bo that goes in a search bar, selects an element, and gets its href link. I am using the following code and I got NullReferenceExceptions thrown. Does anyone know why?

    webBrowser1.Navigate("https://us.octobersveryown.com/search?q=shirt");
    Thread.Sleep(1000);
    String test = webBrowser1.Document.GetElementsByTagName("body")[0].GetAttribute("class");

Thanks, Nickolas

Epoc
  • 7,208
  • 8
  • 62
  • 66
Blar321
  • 5
  • 3

1 Answers1

0

You should put that under DocumentCompleted event and access the document in your handle when it fires:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        string test = webBrowser1.Document.GetElementsByTagName("body")[0].GetAttribute("class");
    }

Your navigation is not yet loading even though you call the Navigate method. That's why it returns null.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57