Wiki
Clone wikiCDP / Home
CDP
.NET bindings for the Chrome DevTools Protocol
Documentation about the protocol can be found at the following:
Protocols
Usage
using System; using System.IO; using CDP; using CDP.Protocols; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); // "using" clause ensures that close the chrome process using (chrome) { chrome.Page.FrameNavigated += (sender, e) => { Console.WriteLine("FrameNavigated: " + e.Frame.Id); }; chrome.Start(); chrome.Page.Enable(); chrome.Page.Navigate("http://google.com"); var png = chrome.Page.CaptureScreenshot(); File.WriteAllBytes("Screenshot.png", png.Data); chrome.Delay(1000); } } }
Enable event receiver
use chrome.[Domain].Enable() to enable receiving events.
class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); chrome.Runtime.ConsoleAPICalled += (sender, e) => { Console.WriteLine("ConsoleAPICalled: {0}", e); }; chrome.Runtime.Enable(); chrome.Page.DomContentEventFired += (sender, e) => { Console.WriteLine("DomContentEventFired: {0}", e.Timestamp); }; chrome.Page.Enable(); chrome.Page.SetDocumentContent( chrome.FrameId, @"<html> <head> <script> console.log('console.log test'); </script> </head> <body> </body> </html>" ); chrome.Delay(1000); } } }
Open new tab
class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); TargetJson target = chrome.OpenNewTab("http://google.com"); HeadlessChrome chrome2 = chrome.Open(target); // use chrome2 to control the new tab } } }
Protocol version
class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); // library version Console.WriteLine(HeadlessChrome.Version); // browser version Console.WriteLine(chrome.GetVersion()); } } }
Update the protocol version of the library
- Download the source code for this repository
- Execute the following code
using CDP; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); ProtocolJson protocol = chrome.GetProtocol(); string outputPath = "Protocols"; ProtocolBuilder.Build(protocol, outputPath); } } }
- Overwrite "Protocols" folder created in the executable file folder to the source code
- Rebuild library
Send a request manually
using CDP; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); // the return value is a JSON string string result = chrome.Send("Browser.close"); Console.WriteLine(result); } } }
Send a request manually with parameters
using CDP; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); // use an anonymous class when specifying parameters string result = chrome.Send( "Page.setDocumentContent", new { frameId = chrome.FrameId, html = "<html><body>Test</body></html>" } ); Console.WriteLine(result); } } }
Send a request manually (asynchronous)
- Asynchronous methods call in APM manner
- Tasks and async/await are not yet supported
using CDP; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); AsyncCallback callback = (IAsyncResult asyncResult) => { // the return value is a JSON string string result = chrome.EndSend(asyncResult); Console.WriteLine(result); }; chrome.BeginSend( "Browser.close", callback, null ); chrome.Delay(1000); } } }
Set timeout
using CDP; class MainClass { public static void Main(string[] args) { HeadlessChrome chrome = new HeadlessChrome( HeadlessChrome.DefaultAppPathMac ); using (chrome) { chrome.Start(); // specify the timeout value in milliseconds chrome.SendTimeout = 1000; // enable throwing exceptions when sending // if not enabled, null is returned chrome.ThrowExceptionOnSend = true; chrome.Page.Crash(); chrome.Delay(2000); } } }
Updated