Nếu bạn có nhu cầu thao tác tự động với website. Mà không muốn dùng cách tà đạo. Sao không thử sử dụng trình duyệt web chrome được tạo ra cho dân developer.
Cùng tìm hiểu cách sử dụng CefSharp trên WPF nhé
Code MainWindow.xaml
<Window x:Class ="CefSharp___WPF.MainWindow"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local ="clr-namespace:CefSharp___WPF"
mc:Ignorable ="d"
xmlns:cef ="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Loaded ="Window_Loaded"
Title ="MainWindow" Height ="550" Width ="980" >
<DockPanel >
<TextBox x:Name ="txbID" DockPanel.Dock ="Top" > </TextBox >
<Button DockPanel.Dock ="Top" Content ="Close" Click ="Button_Click" > </Button >
<Button DockPanel.Dock ="Top" Content ="Show" Click ="Button_Click_1" > </Button >
<cef:ChromiumWebBrowser x:Name ="web" > </cef:ChromiumWebBrowser >
</DockPanel >
</Window >
Code MainWindow.xaml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp;
namespace CefSharp___WPF
{
public partial class MainWindow : Window
{
CookieVisitor cookie = new CookieVisitor();
public MainWindow ()
{
InitializeComponent();
}
void ExecuteScript(string script)
{
web.EvaluateScriptAsync(script);
}
private void Window_Loaded (object sender, RoutedEventArgs e)
{
web.Load("http://www.howkteam.com/" );
}
private void Button_Click (object sender, RoutedEventArgs e)
{
MessageBox.Show(web.Address);
}
string GetHTML(string id)
{
string script = string .Format("var a = document.getElementById('{0}').innerHTML; document.cookie = 'howkteam = ' + a; alert(a);" , id);
ExecuteScript(script);
try
{
Cef.GetGlobalCookieManager().VisitAllCookies(cookie);
foreach (var item in cookie.AllCookies)
{
MessageBox.Show(item.Key + " \\ " + item.Value);
}
}
catch { }
return "" ;
}
private void Button_Click_1 (object sender, RoutedEventArgs e)
{
MessageBox.Show(GetHTML(txbID.Text));
}
}
public class CookieVisitor : ICookieVisitor
{
public CookieVisitor ()
{
IsReady = true ;
}
public bool Visit (Cookie cookie, int count, int total, ref bool deleteCookie)
{
lock (this )
{
if (_cookies.ContainsKey(cookie.Name))
{
_cookies[cookie.Name] = new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
_cookies[cookie.Name].Name = cookie.Name;
_cookies[cookie.Name].Value = cookie.Value;
_cookies[cookie.Name].Path = cookie.Path;
_cookies[cookie.Name].Domain = cookie.Domain;
}
else _cookies.Add(cookie.Name, new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
IsReady = count == total - 1 ;
}
return true ;
}
public void Dispose ()
{
}
Dictionary<string , System.Net.Cookie> _cookies = new Dictionary<string , System.Net.Cookie>();
public Dictionary<string , System.Net.Cookie> AllCookies { get { return _cookies; } }
public bool IsReady { get ; set ; }
public System.Net.Cookie this [string name]
{
get { return _cookies.ContainsKey(name) ? _cookies[name] : null ; }
}
~CookieVisitor()
{
}
}
}
Project
Hãy khoe thành quả của mình ngay phần bình luận nhé.
Đừng quên “Luyện tập – Thử thách – Không ngại khó ”.
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học CefSharp trên WPF C# - [Stream kỹ thuật] dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
em muốn: không click button mà mã js tự chạy ngầm thì phải làm như thế nào ạ
tịt dời
hay
Em mới tham khảo được cách lấy element bằng cách dùng trực tiếp cefsharp, không biết vậy có được không ? (Mọi người xem giùm ạ :)));
Em sử dụng trên trang howkteam
string script = string.Format("document.getElementsByClassName('btn btn-warning')[0].value;"); //em lấy giá trị của nút "giới thiệu"
web.EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;
if (response.Success && response.Result != null)
{
var startDate = response.Result;
string a = Convert.ToString(startDate);
// a chính là giá trị của ElementsByClassName('btn btn-warning')[0].
// xuất ra thông báo để kiểm tra thôi ạ
string alerts = "alert('" + a + "');";
web.EvaluateScriptAsync(alerts);
}
Anh ơi cho em hỏi tí ạ ?
Khi dùng web.EvaluateScriptAsync(script); mà trang web chưa load thì nó báo lỗi ạ. Cụ thể là :
MainWindow.xaml.cs
namespace Googlebrowser
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
// App.Current.MainWindow.Hide();
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
web.Load("http://mmoity.com/12A5");
string script = "setTimeout(function(){document.getElementById('skip_ad_button').click();},40000);";
web.EvaluateScriptAsync(script);
}
}
}
Nó báo lội chỗ in đậm ấy anh, giờ mình xử lí sao ạ ?