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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15835533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:29:17  来源:igfitidea点击:

How to get the value of an input box of the webbrowser control in WPF?

c#htmlwpfbrowser

提问by marc40000

I added Microsoft.mshtml as a reference to my project and came this far:

我添加了 Microsoft.mshtml 作为对我的项目的引用,并走到了这一步:

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

but the 2nd line doesn't work. It says

但第二行不起作用。它说

"error CS0021: Cannot apply indexing with [] to an expression of type 'mshtml.IHTMLElementCollection'"

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

when hovering over "all". How do I access the elements in all?

当悬停在“全部”上时。我如何访问所有元素?

回答by SHSE

Try this:

尝试这个:

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

回答by user2521493

After struggling for a couple of hours this solutions worked for me.

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

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