webBrowser执行js的方法,并返回值,c#后台取值

时间:2022-01-24 06:44:02
      private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(Application.StartupPath + @"\i.html");
txtInfo.Text = webBrowser1.DocumentText;
}
private void button2_Click(object sender, EventArgs e)
{ webBrowser1.Document.InvokeScript("GetCode", new object[] { "","","顶一个","D7D796981B62D318"});
if (webBrowser1.Document.GetElementById("msg") != null)
{
HtmlElement element = webBrowser1.Document.GetElementById("msg");
txtInfo.Text = element.GetAttribute("value");
} }
 function GetCode(lBdId, lDocId, text, sessionID) {

     document.getElementById("msg").value = H2(lBdId + '-' + lDocId + '-' + P(text), 16, sessionID);
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title><script type="text/javascript" src="i.js"></script>
</head>
<body>
<input type=text id=msg />
</body>
</html>

以html页面作为媒介,将返回的值传递到html中的控件,然后在webBrowser中用

 HtmlElement element = webBrowser1.Document.GetElementById("msg");
txtInfo.Text = element.GetAttribute("value")
获得值