CefSharp如何从getElementById('numbers')读取值?

时间:2017-01-31 08:18:46

标签: javascript c# cefsharp

我写了一个简单的网络浏览器。我可以登录该站点,并修改页面的值。但我有一个问题。如何阅读输入表格的值?

我试过了:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Email :- <input type ="text" class="email" /><br/>
Phone:<input type ="text" class="phone" />
South African ID: <input type ="text" class="southafricanid" /><br/>
Passport : <input type ="text" class="passport" />
Birth Date :<input type ="text" class="birthdate" />
<br/>
<input type ="button" class="btnsubmit" value="Validate" />

如何将值返回到label.Test = c?

但返回一个值: System.Threading.Tasks.Taks [CefSharp.JavascriptResponse]

1 个答案:

答案 0 :(得分:0)

您必须使用javascriptResponse的成员:success,result&amp;信息。 以下是您可以在主表单代码中尝试的示例:

private JavascriptResponse JsScriptResult(string script, int timeout)
{
    if (browserTabControl.SelectedIndex == -1)
      {return null;}

    BrowserTabUserControl tabBrowser = (BrowserTabUserControl)browserTabControl.TabPages(browserTabControl.SelectedIndex).Controls(0);
    IFrame frame = tabBrowser.Browser.GetFocusedFrame();
    Task<JavascriptResponse> Task = frame.EvaluateScriptAsync(script, timeout: TimeSpan.FromMilliseconds(timeout));
    Task.Wait();
    return Task.Result;
}

private void test()
{
    JavascriptResponse result;
    // get a list of input elements with an id 
    string script = "var els = document.getElementsByTagName('input');\n var t = [];\nfor (i=0;i<els.length;i++)\n  if (els[i].id) t.push(els[i].id);\n t.join('\\n');";
    result = JsScriptResult(script, 5000);
    if (!result.Success)
    {
        MsgBox(result.Message, MsgBoxStyle.Exclamation, "Script error");
        return;
    }

    // get value for an element with given id
    string id = InputBox(result.Result.ToString, "Id to search ?");
    if (id.Length > 0){
        result = JsScriptResult($"document.getElementById('{id}').value;", 1000);
        MsgBox($"Result = {result.Result}\nMessage = {result.Message}", result.Success ? MsgBoxStyle.Information : MsgBoxStyle.Exclamation, "Script result");
    }
}

或在VB.net中

Private Function JsScriptResult(script As String, timeout As Integer) As JavascriptResponse
   If browserTabControl.SelectedIndex = -1 Then
      Return Nothing
   End If
   Dim tabBrowser As BrowserTabUserControl = CType(browserTabControl.TabPages(browserTabControl.SelectedIndex).Controls(0), BrowserTabUserControl)
   Dim frame As IFrame = tabBrowser.Browser.GetFocusedFrame()
   'Execute extension method
   Dim Task As Task(Of JavascriptResponse) = frame.EvaluateScriptAsync(script, timeout:=TimeSpan.FromMilliseconds(timeout))
   Task.Wait()
   Return Task.Result
End Function
Sub test() Handles ConnexionMdhMenuItem.Click
   Dim result As JavascriptResponse
   ' get a list of input elements with an id 
   Dim script As String = "
     var els = document.getElementsByTagName('input');
     var t = [];
     for (i=0;i<els.length;i++)
       if (els[i].id) t.push(els[i].id);
     t.join('\n');"

   result = JsScriptResult(script, 5000)
   If Not result.Success Then
      MsgBox(result.Message, MsgBoxStyle.Exclamation, "Script error")
      Return
   End If
   'get value for an element with given id
   Dim id As String = Interaction.InputBox(result.Result.ToString, "Id to search ?")
   If id.Length > 0 Then
      result = JsScriptResult($"document.getElementById('{id}').value;", 1000)
      MsgBox($"Result = {result.Result}{vbCr}Message = {result.Message}",
               If(result.Success, MsgBoxStyle.Information, MsgBoxStyle.Exclamation), "Script result")
   End If
End Sub