Search code examples
c#javascriptwpf-controlswebbrowser-controlinvokescript

WebBrowser Control - Invoke Script Problems with JavaScript Button


I am trying to login to a website. I am able to fill out the form correctly with all of the connection information, but I cannot get the page to login. The button is a link without a name or id, so I can't click it directly. I have been working on invokeScript() stuff, but that doesn't seem to work either. This is the button code:

<div style="float:left; width:50px; margin: 0px auto 0px auto; text-align:center;">
    <div id="loginBtn">
        <a href="#" style="text-decoration:none; font-weight:bold; font-size: 10px; color:#FFFFFF;" onClick="javascript: LoginSubmit(event, true);" tabindex="3">
            Log In
        </a>
    </div>
</div>

How can I click a link like that?

I have tried stuff like this:

webBrowserControl.InvokeScript("LoginSubmit", "true" );

webBrowserControl.InvokeScript("LoginSubmit(event, true)"); 

and

webBrowserControl.InvokeScript("LoginSubmit", new object[] { new string[] { "event", "true" } });

Solution

  • You may be missing Document, like so:

    webBrowserControl.Document.InvokeScript(name, args)

    if not try invoking the script with this wrapper method, extract:

    private object MyInvokeScript(string name, params object[] args) 
    { 
        return webBrowserControl.Document.InvokeScript(name, args); 
    }
    
    …
    
    int x = 50; 
    int y = 100; 
    MyInvokeScript("LoginSubmit",x, y);