forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (43 loc) · 1.69 KB
/
Program.cs
File metadata and controls
54 lines (43 loc) · 1.69 KB
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
50
51
52
53
54
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using CefSharp.OffScreen;
using CefSharp.Dom;
using Nito.AsyncEx;
using CefSharp;
namespace Example.ComplexJSObjects
{
class Program
{
[DebuggerDisplay("Title: {Title} Url: {Url}")]
public class Data
{
public string Title { get; set; }
public string Url { get; set; }
public override string ToString() => $"Title: {Title} \nURL: {Url}";
}
public static int Main(string[] args) => AsyncContext.Run(AsyncMain);
static async Task<int> AsyncMain()
{
Console.WriteLine("Navigating to https://stackoverflow.com/questions/tagged/cefsharp");
using var chromiumWebBrowser = new ChromiumWebBrowser("https://stackoverflow.com/questions/tagged/cefsharp");
await chromiumWebBrowser.WaitForInitialLoadAsync();
var devtolsContext = await chromiumWebBrowser.CreateDevToolsContextAsync();
Console.WriteLine("Get all urls from page");
var jsCode = @"() => {
const selectors = Array.from(document.querySelectorAll('.s-post-summary--content-title .s-link'));
return selectors.map( t=> {return { title: t.innerHTML, url: t.href}});
}";
var results = await devtolsContext.EvaluateFunctionAsync<Data[]>(jsCode);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
await devtolsContext.DisposeAsync();
Cef.Shutdown();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return 0;
}
}
}