marc40000
marc40000

Reputation: 3197

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

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

        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'"

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

Upvotes: 3

Views: 9836

Answers (2)

user2521493
user2521493

Reputation: 21

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()

Upvotes: 0

SHSE
SHSE

Reputation: 2433

Try this:

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

Upvotes: 5

Related Questions