使用CEFSharp设置不会显示下拉列表的值

时间:2018-02-07 19:06:30

标签: c# getelementbyid cefsharp

我尝试使用C#和CefSharp在winform上显示Intranet html页面。我可以使用ChromiumWebBrowser的实例打开html页面。我也成功填写了一些文本字段,但设置下拉列表的值似乎不起作用。下面的html片段显示了我想要修改的下拉列表sbo_company和textfield sbo_user。



    <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>
&#13;
&#13;
&#13;

为sbo_user设置I值正在运行,但设置sbo_company不会影响页面。我尝试在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';");
        }
    };            
}

当我用chrome打开页面并输入document.getElementById(&#39; sbo_company&#39;)。value =&#39; SBODEMOAR&#39 ;;在它工作的开发人员工具的控制台中。如何使用CefSharp进行设置? 但是如果我像这样添加Thread.Sleep它会起作用:

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';");
                }

有一些更清洁的解决方案吗?这似乎是一些时间问题。

1 个答案:

答案 0 :(得分:0)

对我有用,希望对您有帮助

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);