如何在WPF中获取webbrowser控件的输入框的值?

我添加了Microsoft.mshtml作为我的项目的参考,并且到目前为止:

mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)webbrowser.Document; string username = document.all["username"].GetAttribute("value"); 

但第二行不起作用。 它说

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

当鼠标hover在“全部”上时。 如何访问所有元素?

试试这个:

 var document = (IHTMLDocument3) webbrowser.Document; var value = document.getElementsByName("username") .OfType() .Select(element => element.getAttribute("value")) .FirstOrDefault(); 

经过几个小时的挣扎,这个解决方案对我有用。

 Dim document = DirectCast(MainBrowser.Document, IHTMLDocument3) Dim formName = document.getElementsByName(AppSettings("myFormName")).OfType(Of IHTMLElement)().Select(Function(element) element.getAttribute("name")).FirstOrDefault()