0

I'm making an Windows Form Application in C# with Visual Studio 2010.

What I want to do, is to go to an page with the webbrowser control, and then insert an value in an input box, and submit the form.

The navigating part isn't that hard, and is already handled. But i can't get the text inserting working...

The problem is that the input doesn't have in ID, only the name.

In JQuery the code will looks like this:

$('input[name="searchstring"]').val("hoi");

But how would that look in C# code? (or combi of C# and Javascript / Jquery)

I already have this code:

HtmlElementCollection playerSearchBox = webBrowserSearchPlayer.Document.GetElementsByTagName("searchstring");
playerSearchBox[0].InnerText = "hoi";

The error that I get with that C# code is as follow:

Value '0' is not valid for 'index'. 'index' should be between 0 and -1. parameters name: index

3
  • 2
    GetElementsByTagName would get the element if you had <searchstring> tag. Commented Nov 30, 2012 at 9:57
  • @L.B, ofcourse, how stupid of me i didn't think about that.. Commented Nov 30, 2012 at 9:58
  • @L.B, if you make an answer of that, and add this to it, i can mark that as the answer ;) HtmlElementCollection playerSearchBox = webBrowserSearchPlayer.Document.GetElementsByTagName("input"); playerSearchBox[2].InnerText = "hoi"; Commented Nov 30, 2012 at 10:14

1 Answer 1

1

It can be something like this.

var input = webBrowserSearchPlayer.Document.GetElementsByTagName("input")
                    .Cast<HtmlElement>()
                    .FirstOrDefault(i => i.GetAttribute("name") == "searchstring");
Sign up to request clarification or add additional context in comments.

2 Comments

Well, thank's for thinking, but is that code better than this: HtmlElementCollection playerSearchBox = webBrowserSearchPlayer.Document.GetElementsByTagName("input"); playerSearchBox[2].InnerText = "hoi";
@MarcMeesters I think not hardcoding 2 would be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.