C# webview2获取网页HTML的绝招

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 vsphere
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void button1_Click(object sender, EventArgs e)
        {
            this.webView21.Source = new System.Uri(textBox1.Text, System.UriKind.Absolute);
        }
        private async void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
  
            object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");//第一次获取没法获取后端的数据
            textBox2.Text = obj.ToString();
        }
  
        private async void button2_Click(object sender, EventArgs e)
        {
            object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");//等一会儿获取HTML就能看到后端数据了
            textBox2.Text = obj.ToString();
        }
  
        private async void button3_Click(object sender, EventArgs e)
        {
            object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerText");//获取页面的文本
            textBox2.Text = obj.ToString();
        }
  
        private async void button4_Click(object sender, EventArgs e)
        {
            object obj = await webView21.CoreWebView2.ExecuteScriptAsync("$('.f3')[0].innerHTML");//jquery获取页面内容
            textBox2.Text = obj.ToString();
        }
    }
}

  

posted @   多见多闻  阅读(1941)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示