0

If I have an application [c#] with a web browser control in it and I want to fill the first text area with information on a click off a button. How would I do it?

How would I auto fill a textArea in C# with a web browser if it has

 <textarea class="profile" name="message"></textarea><br />

But no ID field set?

[C#]

private void messageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        webBrowser1.Document
           .GetElementsByTagName("textarea")
           .GetElementsByName("message")[0]
           .SetAttribute("value", "Something");
      //  HtmlDocument doc = this.webBrowser1.Document;
     //   doc.GetElementsByTagName("textarea")[0].SetAttribute("Value", "a");
    }


    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        MessageBox.Show("LoadedLux");
        webBrowser1.Document
            .GetElementsByTagName("input")
            .GetElementsByName("q")[0]
            .SetAttribute("value", "Something");
    }
  • 1
    What are you talking about? – SLaks Apr 23 '13 at 02:56
  • How would you do that id ID is set? Show that sample and it may help to find solution for your case. – Alexei Levenkov Apr 23 '13 at 02:57
  • So for example if it was :
    then I could use document.GetElementById("box").SetAttribute("Value", "ThisNewBox"); But What if there was no ID SET?
    – user1999321 Apr 23 '13 at 03:01
  • @user1999321, if you have access to code, can you add id to textarea and use your code above from your comment to access it? If you want to access from C# code you need id and runat="server" tag. – rs. Apr 23 '13 at 03:04
  • @rs. I don't have access to the code. I can't change it. That would defeat the purpose of this question. – user1999321 Apr 23 '13 at 03:07
  • Are you sure you are talking about C# code, not JavaScript? Or maybe you are talking about WebBrowser control? – Alexei Levenkov Apr 23 '13 at 03:10
  • Because it's an auto fill. Like a program that automatically logs me in to a WEBSITE when I click a button? **** GUYS PLEASE SEE UPDATED QUESTION. I THINK ITS MORE CLEAR NOW **** – user1999321 Apr 23 '13 at 03:10
  • @AlexeiLevenkov Yes! Web Browser Control – user1999321 Apr 23 '13 at 03:11
  • @user1999321, you expect us to answer question that was incomplete and stop using CAPS – rs. Apr 23 '13 at 03:11
  • Okay I'm sorry. But Yes it's the C# WebBrowser Control... – user1999321 Apr 23 '13 at 03:12
  • I think best option is to use [Watin](http://watin.org/) or check this SO solution [here](http://stackoverflow.com/a/2540531/125551) – rs. Apr 23 '13 at 03:20
  • @ALL I think I should delete this question and asked it again. So it's more clear guys? – user1999321 Apr 23 '13 at 03:20
  • No need to delete it. Just edit it. It is in fact a good question. The problem with it is that you were not clear and most people assumed you were working with a web application. – Matt Apr 23 '13 at 03:24

1 Answers1

2

Have you tried this?:

webBrowser1.Document.GetElementsByTagName("textarea").GetElementsByName("message")[0].SetAttribute("Value", "ThisNewBox");

UPDATE

I have just tried the following on Google.com and it's working fine:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document
                .GetElementsByTagName("input")
                .GetElementsByName("q")[0]
                .SetAttribute("value", "Something");
        }
Matt
  • 6,787
  • 11
  • 65
  • 112
  • @ Matt .. it gives this error : Value of '0' is not valid for 'index'. 'index' should be between 0 and -1. Parameter name: index – user1999321 Apr 23 '13 at 03:24
  • See my update. You can test it by setting your URL to http://www.google.com and using the above code. Then just modify it to your own scenario – Matt Apr 23 '13 at 03:30
  • PS - I see you're new here. Welcome. Please remember to accept this as 'answer' if it helps solve your problem. – Matt Apr 23 '13 at 03:32
  • The google one worked but when I try it on this website, it still gives the same error of index. I changed the input to textarea and q to message... – user1999321 Apr 23 '13 at 03:36
  • That error you mentioned above might happen if the document has not yet loaded before you start querying it. Move your code to the `DocumentCompleted` event handler or some other appropriate location when the document (web page) has finished loading. – Matt Apr 23 '13 at 03:37
  • If you still get problems, update your question with more code so we can better see what's going on. – Matt Apr 23 '13 at 03:38
  • Okay, if i use the same method you are using. How would I get the method to execute? – user1999321 Apr 23 '13 at 03:40
  • I don't understand what you mean. Like I said.. update your question with more code, so we can have a clearer picture. – Matt Apr 23 '13 at 03:44
  • I see, but unfortunately I don't have a clue what the problem is. What I have given you should be a good start. You'll have to debug it and see what's going on. Put `webBrowser1.Document.GetElementsByTagName("textarea")` into your watch window and see if it returns anything. Wish I could be of more help, but I think you'll have to take it from here yourself. – Matt Apr 23 '13 at 03:53
  • Matt just one more question could it be that "message" box doesnt exist? so can I do just ANY text area it finds set this value? – user1999321 Apr 23 '13 at 03:56
  • @user1999321, that is possible. Inspect the DOM; check if that element exists or not. – Matt Apr 24 '13 at 00:42
  • @ Matt : The View Source shows the element – user1999321 Apr 24 '13 at 01:17
  • @user1999321, it might be helpful if you paste your DOM into the question. If you do so, please mark it as EDIT or UPDATE so viewers can see it is new. Example: **EDIT** . Seeing your DOM might help give us a clue. – Matt Apr 25 '13 at 00:11