简体   繁体   中英

Using CEFSharp setting the value of a dropdown list does not get displayed

I try do display an intranet html page on a winform using C# and CefSharp. I can open the html page using an instance of ChromiumWebBrowser. I also succeeded filling some text fields, but setting the value for a dropdown list seems not to work. The html fragment below show the dropdownlist sbo_company and the textfield sbo_user that I want to modify.

 <table class="sbo_layer"> <tr> <td>Company</td> <td> <input value="Refresh" id="refresh_company" type="button" style="width:66px"> <select id="sbo_company" style="width:63%"><option value="SBODEMOCL" selected="">SBODEMOCL</option><option value="SBODEMOAR">SBODEMOAR</option></select> <input type="text" id="sbo_custom_company" style="display:none"> </td> </tr> <tr> <td>User ID</td> <td><input type="text" id="sbo_user"></td> </tr> </tbody></table>

Setting I value for sbo_user is working but setting sbo_company does not affect the page. I try to do the following in C#:

public void InitializeChromium()
{
    CefSettings settings = new CefSettings();
    // Initialize cef with the provided settings

    Cef.Initialize(settings);
    // Create a browser component
    chromeBrowser = new ChromiumWebBrowser("http://x.x.x.x/");
    // Add it to the form and fill it to the form window.
    this.Controls.Add(chromeBrowser);

    chromeBrowser.Dock = DockStyle.Fill;

    //Wait for the MainFrame to finish loading
    chromeBrowser.FrameLoadEnd += (sender, args) =>
    {
        //Wait for the MainFrame to finish loading
        if (args.Frame.IsMain)
        {
            args.Frame.ExecuteJavaScriptAsync("document.getElementById('sbo_user').value = 'manager';");
            args.Frame.ExecuteJavaScriptAsync("document.getElementById('sbo_company').value = 'SBODEMOAR';");
        }
    };            
}

When I open the page with chrome and enter document.getElementById('sbo_company').value = 'SBODEMOAR'; in the console of the developer tools it works. How do I set this with CefSharp? But it works if I add Thread.Sleep like this:

if (args.Frame.IsMain)
                {
                    Thread.Sleep(2000);
                    args.Frame.ExecuteJavaScriptAsync("document.getElementById('sbo_user').value = 'manager';");
                    args.Frame.ExecuteJavaScriptAsync("document.getElementById('sbo_company').value = 'SBODEMOAR';");
                }

Is there some cleaner solution? It seems to be some timing issue.

It work for me, hope will help you

string script = @"var tm = 'value select';
                  var select = document.getElementById('idOfSelect');
                  for (var i = 0; i < select.children.length; i++)
                  {
                      var v = select.children[i].text;
                      var cp = tm.localeCompare(v);
                      if (cp == 0)
                      {
                           select.children[i].selected = true;
                      }
                  }";
browser.EvaluateScriptAsync(script);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM