如何在 C# 或 WPF 中执行 javascript 函数 [英] How can I execute javascript function in C# or WPF

查看:48
本文介绍了如何在 C# 或 WPF 中执行 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VS2015 中使用 C# .net 4.5 并尝试抓取网站.HTML 表格到数据.对于使用winform C#的web浏览器控件一,因为听说比WPF控件功能多.

I'm using C# .net 4.5 with VS2015 and try to do scraping the website. HTML table to data. For the web browser control using winform C# one, because I heard that one has more feature than WPF control.

这是我尝试解析的网站.(韩国音乐流媒体网站,如苹果音乐)http://www.melon.com/mymusic/like/mymusiclikesong_list.htm?memberKey=7605160还有一个如下所示的翻页脚本.

Here's the website which I try to parse. (Korean music streaming site like apple music) http://www.melon.com/mymusic/like/mymusiclikesong_list.htm?memberKey=7605160 and there's a script for turning the page as shown below.

javascript:pageObj.sendPage('21');

表示从数据列表的第21条开始显示数据表.但我不知道如何在我的 C# 浏览器中运行该脚本.我尝试使用带参数的 InvokeScript 运行,但找不到脚本.据我所知,为了执行对象包装函数,我可以像代理一样在加载的 HTML 文档中注入一个函数.但它也失败了.经过多次尝试,我注意到该功能可以通过 chrome 控制台(F12)轻松执行.是否有任何源代码片段或第 3 方库可以像 chrome 控制台一样在 C# 中运行 javascript?或者即使我必须实施它,请给我任何提示.

that means to show the data table since 21 of the data list. but I don't know how to run that script inside of my C# webbrowser. I tried to run with InvokeScript with parameters but it couldn't find the script. As far as I've heard in order to execute object wrapped function, I can inject a function inside of loaded HTML document like a proxy. but it failed as well. After many attempts, I've noticed the function can be executed by chrome console(F12) just easily. Is there any source snippet or 3rd party library to run javascript in C# just like chrome console? or even if I have to implement it, please give me any tips.

推荐答案

javascript:pageObj.sendPage('21'); 是一个可以导航到的网址.Web 浏览器实际上并不导航到脚本,而是执行脚本.所以你可以简单地使用 Navigate WebBrowserControl:

The javascript:pageObj.sendPage('21'); is a url which you can navigate to. The web-browser doesn't actually navigate to the script, instead it executes the script. So you can simply use Navigate method of WebBrowserControl:

this.webBrowser1.Navigate("javascript:pageObj.sendPage('21');");

这篇关于如何在 C# 或 WPF 中执行 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆