gpt4 book ai didi

c# - 在 C# webBrowser 控件中调用 Javascript 函数

转载 作者:IT王子 更新时间:2023-10-29 04:16:33 30 4
gpt4 key购买 nike

我在 C# 中使用 webBrowser 控件加载网页,需要调用返回字符串值的 JavaScript 函数。我得到了使用 InvokeScript 方法的解决方案,我尝试了很多,但都失败了。

最佳答案

你能具体说明是什么失败了吗?

我下面的示例由一个带有 WebBrowser 和一个 Button 的表单组成。

最后名为 y 的对象有一句“我做到了!”。所以对我来说它很管用。

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

webBrowser1.DocumentText = @"<html><head>
<script type='text/javascript'>
function doIt() {
alert('hello again');
return 'i did it!';
}
</script>
</head><body>hello!</body></html>";

}

private void button1_Click(object sender, EventArgs e)
{
object y = webBrowser1.Document.InvokeScript("doIt");
}
}

关于c# - 在 C# webBrowser 控件中调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1437251/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com