最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Getting textbox values from a CefSharp browser using javascript - Stack Overflow

programmeradmin2浏览0评论

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 es 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 plicated going on?

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 es 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 plicated going on?

Share Improve this question asked Feb 9, 2017 at 11:51 jardantuanjardantuan 5451 gold badge5 silver badges14 bronze badges 2
  • Your probably executing your script too early, see the general usage guide for details. – amaitland Commented Feb 9, 2017 at 11:54
  • The script is executed when a button is clicked, so it doesn't make sense that the page couldn't be loaded if I can wait as long as I want – jardantuan Commented Feb 9, 2017 at 12:11
Add a ment  | 

2 Answers 2

Reset to default 6

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.

   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();
    }
发布评论

评论列表(0)

  1. 暂无评论