如何在托管WebBrowser控件的应用程序中调用由WebBrowser控件查看的页面中的JavaScript函数?

时间:2022-11-11 23:11:59

I've created a C# Desktop Application which depends on web crawler idea.

我创建了一个C#桌面应用程序,它依赖于Web爬虫的想法。

I made my application using web browser control to open a website and login progmatically and redirect to the specific page which have a gridview with all user's data that I want to collect...

我使用Web浏览器控件创建了我的应用程序来打开一个网站并按计划登录并重定向到具有gridview的特定页面,其中包含我要收集的所有用户数据...

But the issue here username in the gridview click fired by JavaScript function. I know its name but not how to call it inside desktop application.

但是这里的问题是网格视图中的用户名点击了JavaScript函数。我知道它的名字,但不知道如何在桌面应用程序中调用它。

What are the namespaces or DLLs that would allow me to do so?

允许我这样做的命名空间或DLL是什么?

2 个解决方案

#1


I think this should help you out:

我认为这可以帮助你:

http://www.west-wind.com/WebLog/posts/493536.aspx

EDIT: Based on that link, here's a small sample app. Add a Button and a WebBrowser control to a Windows Form and add this code:

编辑:基于该链接,这是一个小样本应用程序。将Button和WebBrowser控件添加到Windows窗体并添加以下代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.DocumentText = @"<html>
<body>
<script type = ""text/javascript"">
function ShowMessage(text) {
   alert(text);
}
</script>
</body>
<input type=""button"" onclick=""ShowMessage('From JavaScript')"" value=""Click me""/>
</html>";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.InvokeScript("ShowMessage",new object[]{"From C#"});
        }
    }

If you click the button in the browser, it'll show the javascript message, if you click the button in the windows form, it'll show the C# message.

如果单击浏览器中的按钮,它将显示javascript消息,如果单击Windows窗体中的按钮,它将显示C#消息。

#2


Have you thought of just using the following to run script on page using WebBrowser control?

你有没有想过使用以下内容使用WebBrowser控件在页面上运行脚本?

' In VB - but easy to convert to C# as its pretty much the same thing :).
Dim sScript As String, sLanguage As String
sLanguage = "JScript"
sScript = "MyJavaScriptToRun();" ' you can run many lines by delimiting them with ;

WebBrowser1.Document.parentWindow.execScript sScript, sLanguage

Let me know how you get along. In the .NET versions of the WebBrowser control, there are additional methods to run script, I think there is a .RunScript type method (can't recall exact name but you will notice it once you see it in intellisense).

让我知道你是如何相处的。在WebBrowser控件的.NET版本中,还有其他运行脚本的方法,我认为有一个.RunScript类型的方法(不能回忆起确切的名称,但是一旦你在intellisense中看到它就会注意到它)。

Let me know if you have any further questions.

如果您有任何其他问题,请与我们联系。

#1


I think this should help you out:

我认为这可以帮助你:

http://www.west-wind.com/WebLog/posts/493536.aspx

EDIT: Based on that link, here's a small sample app. Add a Button and a WebBrowser control to a Windows Form and add this code:

编辑:基于该链接,这是一个小样本应用程序。将Button和WebBrowser控件添加到Windows窗体并添加以下代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.DocumentText = @"<html>
<body>
<script type = ""text/javascript"">
function ShowMessage(text) {
   alert(text);
}
</script>
</body>
<input type=""button"" onclick=""ShowMessage('From JavaScript')"" value=""Click me""/>
</html>";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.InvokeScript("ShowMessage",new object[]{"From C#"});
        }
    }

If you click the button in the browser, it'll show the javascript message, if you click the button in the windows form, it'll show the C# message.

如果单击浏览器中的按钮,它将显示javascript消息,如果单击Windows窗体中的按钮,它将显示C#消息。

#2


Have you thought of just using the following to run script on page using WebBrowser control?

你有没有想过使用以下内容使用WebBrowser控件在页面上运行脚本?

' In VB - but easy to convert to C# as its pretty much the same thing :).
Dim sScript As String, sLanguage As String
sLanguage = "JScript"
sScript = "MyJavaScriptToRun();" ' you can run many lines by delimiting them with ;

WebBrowser1.Document.parentWindow.execScript sScript, sLanguage

Let me know how you get along. In the .NET versions of the WebBrowser control, there are additional methods to run script, I think there is a .RunScript type method (can't recall exact name but you will notice it once you see it in intellisense).

让我知道你是如何相处的。在WebBrowser控件的.NET版本中,还有其他运行脚本的方法,我认为有一个.RunScript类型的方法(不能回忆起确切的名称,但是一旦你在intellisense中看到它就会注意到它)。

Let me know if you have any further questions.

如果您有任何其他问题,请与我们联系。