简体   繁体   English

如何获取WebBrowser控件的URL

[英]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 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. 此外,它必须在计时器中工作,以便可以检查。

The Url Property will remain null untill the control is rendered so use this: Url属性将保持为null直到控件呈现为止,请使用以下命令:

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: 并在您的按钮Click事件添加:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

I thought id comment on this, I literally took your 我认为id就此发表评论,我真的把你的

"webBrowser1.Url.AbsoluteUri;"

and in my case im using a combotextbox so Double click your browser form and it will take you to the even handler, i just put 在我的情况下我使用一个combotextbox所以双击你的浏览器表单,它将带你到偶数处理程序,我只是把

"combobox1.text= webBrowser1.Url.AbsoluteUri;"

and it works for me now. 它现在对我有用。 You got me on the time, but whatever you need to check, check for the combobox1.text or whatever you are using for your url's 你得到了我的时间,但无论你需要检查什么,检查combobox1.text或你用于网址的任何东西

if your browser1 is chromiumwebbrowser, then use 如果您的browser1是chromiumwebbrowser,那么请使用

    string url = browser1.Address;

call the url and you will get it. 拨打网址即可获得。

probably your webBrowser1.Url is null try below to get url 可能你的webBrowser1.Urlnull尝试下面获取url

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

Well you didn't set any url (no page is loaded inside the web browser). 好吧,你没有设置任何网址(网页浏览器中没有加载任何页面)。 You could try this: 你可以试试这个:

webBrowser1.Url = new Uri("http://www.google.com", UriKind.Absolute); webBrowser1.Url = new Uri(“http://www.google.com”,UriKind.Absolute);

And get the url this way: webBrowser1.Url.ToString(); 并以这种方式获取url:webBrowser1.Url.ToString();

Wait for the page to load and the press then button. 等待页面加载,然后按下按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM