Search code examples
c#javascriptwebkittextinput

Webkit.net and own javascript


Well, the question is very simple.

I've got an webkit browser in an class file generated this way:

public static WebKit.WebKitBrowser mainBrowser = new WebKitBrowser();

Now I'm trying to login on an website automaticly. But the input fields aren't filled in.

I'm using this code to get to the point where the data needs to be filled in:

public void loginthen()
{
    this.Controls.Add(globalVars.mainBrowser);
    globalVars.mainBrowser.DocumentCompleted += 
        new WebBrowserDocumentCompletedEventHandler(mainBrowser_DocumentCompleted);
    globalVars.mainBrowser.Navigate("http://www.somesite.com/");                
}

void mainBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var send = sender as WebKit.WebKitBrowser;
    if (send.Url == e.Url)
    {
        MessageBox.Show("Inloggen");
        globalVars.mainBrowser.Document.GetElementById("user").TextContent = "User Name";
    }
}

The MessageBox is shown, and i get no error, but the input field isn't filled in.

The HTML of the input fields is this:

<input id="user" name="user" class="text" type="text" value="" onkeydown="if((e=window.event||event) &amp;&amp; e.keyCode == 13 &amp;&amp; $('#user').val() &amp;&amp; $('#password').val()) $('#login_button').trigger('click');">

So my thought was... Am I using the TextContent wrong?

And can this (and triggering the button and so on) not be much easier be done with javascript?

So I googled and searched for it, but the only thing that I can find is how to call C# function from javascript or how to call an javascript function from C#. But I don't own these sites, so that won't work...

So what are your thoughts about it?


Solution

  • Instead of:

    globalVars.mainBrowser.Document.GetElementById("user").TextContent = "User Name";
    

    you should use:

    globalVars.mainBrowser.Document.GetElementById("user").SetAttribute("value", "User Name");
    

    You may use the TextContent property to set the content of a HTML tag such as <p />, <span /> or maybe <div />.

    globalVars.mainBrowser.Document.GetElementById("myDiv").TextContent = "some div content";