1、Winform项目中安装CefSharp
1)使用Nuget安装
搜索cefsharp =》找到CefSharp.WinForms =》点击安装
2)手动下载编译好的文件添加引用
- CefSharp v51 x86 (for 32-bit apps)
- CefSharp v51 x64 (for 64-bit apps)
添加以下文件引用:
CefSharp.dll
CefSharp.Core.dll
CefSharp.WinForms.dll
3)下载源代码自己编译需要的dll
代码地址:https://github.com/cefsharp/CefSharp/releases
添加以下文件引用:
CefSharp.dll
CefSharp.Core.dll
CefSharp.WinForms.dll
注意:3种方法选一种即可。
2、获取指定的Iframe的代码
this.browser.GetBrowser().GetFrame("tabs_portal_5_iframe").EvaluateScriptAsync("document.getElementById(\"block_3\").click()").Result;
3、测试代码
using CefSharp; using CefSharp.WinForms; 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 MyBrowser { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void Mydelegate(); public ChromiumWebBrowser browser; public void InitBrowser() { Cef.Initialize(new CefSettings()); browser = new ChromiumWebBrowser("https://oa.tongda2000.com/general/index.php?isIE=0&modify_pwd=0"); this.Controls.Add(browser); browser.Dock = DockStyle.Fill; browser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameLoadEndHandle); } public void FrameLoadEndHandle(object sender, FrameLoadEndEventArgs e) { //MessageBox.Show("count = " + e.Browser.GetFrameCount()); //MessageBox.Show("e.Url = " + e.Url); //MessageBox.Show("e.Frame.Url = " + e.Frame.Url); if (e.Frame.Url.IndexOf("/portal/personal/") > -1) { var frame = e.Frame; Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(9000); this.browser.Invoke(new Action(() => { var res = this.browser.GetBrowser().GetFrame("tabs_portal_5_iframe").EvaluateScriptAsync("document.getElementById(\"block_3\").click()").Result; })); }); //object o = res.Result;document.getElementById("block_3").click() // MessageBox.Show(e.Frame.IsValid.ToString()); //MessageBox.Show(e.Frame.Browser.IsLoading.ToString()); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Cef.Shutdown(); } } }