单击使用C#WebBrowser的href按钮/链接

时间:2017-04-12 15:47:46

标签: c# html web webbrowser-control

我正在尝试点击网站上的href链接,这会引导我访问网站的另一个页面,这是我的代码:

private const string baseURL = "https://abv.bg/";

public Form1()
{
    InitializeComponent();
    localWebBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
    localWebBrowser.Navigate(baseURL);
}

private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    MessageBox.Show(localWebBrowser.Url.ToString());
    HtmlElement lostPasswordButton = ObtainHtmlElementById(localWebBrowser, "lostPasswordLink");
    if (lostPasswordButton == null)
    {
        throw new InvalidOperationException($"Couldn't find {nameof(lostPasswordButton)}");
    }
    lostPasswordButton.InvokeMember("click");
}

private HtmlElement ObtainHtmlElementById(WebBrowser webBrowser, string elementName)
{
    return webBrowser.Document.GetElementById(elementName);
}

这是按钮的html:

enter image description here

这是我得到的错误:

enter image description here

0 个答案:

没有答案