How to get the URL of a WebBrowser control

28,776

Solution 1

probably your webBrowser1.Url is null try below to get url

    string url = "";
    if (webBrowser1.Url != null)
    {
        url = webBrowser1.Url.AbsoluteUri;
    }
    if (url == "www.google.com")
    {
        label9.Text = url;
    }

Solution 2

The Url Property will remain null untill the control is rendered so use this:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
     if (webBrowser1.Url.ToString() == "www.google.com") {
          label9.Text = webBrowser1.Url.ToString();
     }
}

And in your button Click event add:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
Share:
28,776
Ian Lundberg
Author by

Ian Lundberg

Updated on July 11, 2020

Comments

  • Ian Lundberg
    Ian Lundberg almost 4 years
            if (webBrowser1.Url.AbsoluteUri == "www.google.com")
            {
                label9.Text = webBrowser1.Url.AbsoluteUri;
            }
    

    This is my current code. When I press the button to run this I get the error.

    Object reference not set to an instance of an object.

    And I don't know why it does that or how to fix it. Any help will be great.

    Also It have to work in a timer so that it can be checked.

  • Ian Lundberg
    Ian Lundberg about 12 years
    how would i implement that into a timer?
  • ionden
    ionden about 12 years
    In your button click event handler register DocumentCompleted