How to get the url of a WebBrowser control
if (webBrowser1.Url.AbsoluteUri == "www.google.com")
{
label9.Text = webBrowser1.Url.AbsoluteUri;
}
This is my current code. When I click the button to run, I get an error.
An object reference is not set on an object instance.
And I don't know why he does it or how to fix it. Any help would be great.
It also needs to run on a timer so that it can be checked.
maybe yours webBrowser1.Url
- null
try below to get url
string url = "";
if (webBrowser1.Url != null)
{
url = webBrowser1.Url.AbsoluteUri;
}
if (url == "www.google.com")
{
label9.Text = url;
}
The property Url
will remain null
until 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);
Well, you didn't provide the url (the page doesn't load in the web browser). You can try this:
webBrowser1.Url = new Uri ("http://www.google.com", UriKind.Absolute);
And get the url like this: webBrowser1.Url.ToString ();
Wait for the page to load and then click the button.
I thought id was a comment on this, I literally took your
"webBrowser1.Url.AbsoluteUri;"
and in my case i am using combotextbox so double click your browser form and it will take you to the even handler, i will just put
"combobox1.text= webBrowser1.Url.AbsoluteUri;"
and now it works for me. You got me in time, but all you need to check is check on combobox1.text or whatever you use for your url.
if your browser1 is chromiumwebbrowser then use
string url = browser1.Address;
call the url and you will get it.