如何在WPF中获取Web浏览器控件的输入框的值?

mar*_*000 3 html c# browser wpf

我添加了Microsoft.mshtml作为对我的项目的引用,到目前为止:

        mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)webbrowser.Document;
        string username = document.all["username"].GetAttribute("value");
Run Code Online (Sandbox Code Playgroud)

但是第二行不起作用。它说

“错误CS0021:无法将[]索引应用于类型'mshtml.IHTMLElementCollection'的表达式”

将鼠标悬停在“全部”上时。如何访问所有元素?

SHS*_*HSE 5

尝试这个:

var document = (IHTMLDocument3) webbrowser.Document;
var value =
    document.getElementsByName("username")
            .OfType<IHTMLElement>()
            .Select(element => element.getAttribute("value"))
            .FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)