jardantuan
jardantuan

Reputation: 535

Getting textbox values from a CefSharp browser using javascript

I've got a winforms app that has a ChromiumWebBrowser control and some basic windows controls. I want to be able to click a button, call javascript to get the value of a textbox in the browser, and copy the returned value to a textbox in the winforms app. Here is my code:

string script = "(function() {return document.getElementById('Email');})();";

string returnValue = "";

var task = browser.EvaluateScriptAsync(script, new { });

await task.ContinueWith(t =>
    {
        if (!t.IsFaulted)
        {
            var response = t.Result;
            if (response.Success && response.Result != null)
            {
                returnValue = (string)response.Result;
            }
        }
    });

txtTarget.Text = returnValue;

The result that comes back however is just "{ }". I've loaded the same web page in Chrome and executed the same javascript in the dev tools and I get the textbox value as expected.

The demo I looked at had sample code, simply "return 1+1;", and when I tried that I was getting the value "2" returned instead of "{ }". Interestingly, when I tried

string script = "(function() {return 'hello';})()";

I was still getting "{ }", almost as though this doesn't work with strings.

I've been scratching my head at this for a while and haven't been able to figure out how to solve this. Am I making a very basic syntax error or is there something more complicated going on?

Upvotes: 1

Views: 8682

Answers (2)

321vcxz
321vcxz

Reputation: 21

   public void SetElementValueById(ChromiumWebBrowser myCwb, string eltId, string setValue)
    {         
        string script = string.Format("(function() {{document.getElementById('{0}').value='{1}';}})()", eltId, setValue);
        myCwb.ExecuteScriptAsync(script);            
    }

    public string GetElementValueById(ChromiumWebBrowser myCwb, string eltId)
    {        
        string script = string.Format("(function() {{return document.getElementById('{0}').value;}})();",
             eltId);
        JavascriptResponse jr = myCwb.EvaluateScriptAsync(script).Result;
        return jr.Result.ToString();
    }

Upvotes: 0

jardantuan
jardantuan

Reputation: 535

So I think I've figured it out:

string script = "(function() {return document.getElementById('Email').value;})();";
string returnValue = "";

var task = browser.EvaluateScriptAsync(script);

await task.ContinueWith(t =>
{
    if (!t.IsFaulted)
    {
        var response = t.Result;

        if (response.Success && response.Result != null)
        {
            returnValue = response.Result.ToString();
        }
    }
});  

txtTarget.Text = returnValue;

Removing the args object from EvaluateScriptAsync seemed to fix the issue. Not sure what the problem was - perhaps it was trying to run the javascript function with an empty args object when it shouldn't take any parameters?

Either way, it's resolved now.

Upvotes: 6

Related Questions