首页新闻找找看学习计划

关于c#嵌入微信网页端,并且获取微信群用户

0
悬赏园豆:10 [待解决问题]

微信网页端https://wx.qq.com/?lang=zh_CN
winform的WebBrowser控件,可以方便的载入页面并执行一些操作,我们可以注入自己的js脚本在页面中执行,方法如下:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");
另外如果你的脚本非常简单也可以用下面的方法:
string jCode = "alert("Hello");"
// or any combination of your JavaScript commands
// (including function calls, variables... etc)

// WebBrowser webBrowser1 is what you are using for your web browser
webBrowser1.Document.InvokeScript("eval", new object[] { jCode });
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//webBrowser1.ObjectForScripting = this;
}

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.IsScriptingEnabled = true;//启用js函数调用
        //string str = webBrowser1.StringByEvaluatingJavaScriptFromString("test1()");
        richTextBox1.Text = "加载完成";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //var names = document.querySelectorAll("p.nickname.ng-binding");
        //     var output ="",length = names.length

        //     for(var i=0;i<length;i++){
        //         output += names[i].innerHTML +"\n";
        //     }
        //     console.log(output);
        //     copy(output);
        webBrowser1.IsScriptingEnabled = true;//启用js函数调用
        WebKit.DOM.Document doc = webBrowser1.Document;
        WebKit.DOM.NodeList nodelist = doc.GetElementsByTagName("body");
        WebKit.DOM.Node node = nodelist[0];
        WebKit.DOM.Element ele = webBrowser1.Document.CreateElement("script");
        ele.TextContent = "function sayHello() {return '1213';}";
        //ele.TextContent = "function sayHello() { var names = document.querySelectorAll('p.nickname.ng - binding');var output ='',length = names.length;for(var i=0;i<length;i++){ output += names[i].innerHTML +'\n'; }   return output; }";
        node.AppendChild(ele);
        //获取用户
        WebKit.DOM.NodeList plist = doc.GetElementsByTagName("p");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < plist.Length; i++)
        {
            //string nodeName = plist[i].NodeName;
            //string nodeValue = plist[i].NodeValue;
            //string localName = plist[i].LocalName;
            //bool isHasChild = plist[i].HasChildNodes;

            string content = plist[i].TextContent;
            //sb.Append("nodeName:"+nodeName+"==nodeVale:"+nodeValue+"==content:"+content+ "==localName:" + localName+ "\nisHasChild:" + isHasChild);
            if (content.Trim().Length == 0 || content == "使用手机微信扫码登录" || content == "网页版微信需要配合手机使用") {
                continue;
            }
            if (content.Contains("山西老乡俱乐部") || content.Contains("天津吃喝玩乐团") || content.Contains("下载微信 PC 版") ||content == "暂时没有新消息" || content == "未选择聊天" || content == "请检查你的网络设置 点击重连" || content == "二维码失效,点击刷新" || content == "请在手机点击确认登录")
            {
                continue;
            }
            if (content == "加载中..."|| content == "暂无文章..." || content == "1:01" || content == "0:06" || content == "请检查你的网络设置 点击重连" || content.Contains("Tencent Inc")|| content == "正在获取最近的聊天...")
            {
                continue;
            }
            sb.Append("\ncontent:" + content);

        }

       string text = webBrowser1.DocumentText;
        bool b = webBrowser1.IsScriptingEnabled;
        //Object obj = webBrowser1.Document.InvokeScriptMethod("document.querySelectorAll('p.nickname.ng - binding')", "");
        Object obj = webBrowser1.Document.InvokeScriptMethod("sayHello",new Object[] { });
        richTextBox1.Text = "按钮点击;" + b + "==" + obj.ToString() + ";"+sb.ToString();//+ nicklist.Length + "=" + nodelist[0].ToString();
    }
}

}
目前能获取到信息,但是想在页面执行javaScript如:
var names = document.querySelectorAll("p.nickname.ng-binding");
var output ="",length = names.length

            for(var i=0;i<length;i++){
             output += names[i].innerHTML +"\n";
           }
         console.log(output);
          copy(output);

会报这个错误:
C# winform 对 COM 组件的调用返回了错误 HRESULT E_FAIL。
希望大家知道这个错误的帮助一下

程旭清的主页 程旭清 | 初学一级 | 园豆:184
提问于:2019-08-30 09:14
< >
分享
所有回答(1)
0

放弃webbrowser,cef才是好归宿,很多年前我就放弃使用这玩意儿了,如果就是 取取数据 或者 测试一下的 简易操作,你可以用webdriver玩玩。

花飘水流兮 | 园豆:9807 (大侠五级) | 2019-08-30 11:53

能不能给个小案例?

支持(0) 反对(0) 程旭清 | 园豆:184 (初学一级) | 2019-08-30 12:28

@程旭清: https://github.com/cefsharp/CefSharp/wiki/General-Usage

支持(0) 反对(0) 花飘水流兮 | 园豆:9807 (大侠五级) | 2019-08-30 12:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册