forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebPageHelper.cs
More file actions
59 lines (46 loc) · 1.48 KB
/
WebPageHelper.cs
File metadata and controls
59 lines (46 loc) · 1.48 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
55
56
57
58
59
using HtmlAgilityPack;
namespace BotSharp.Plugin.WebDriver;
public static class WebPageHelper
{
public static HtmlNode GetElement(string html, string selector)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
return htmlDoc.DocumentNode.SelectSingleNode(selector);
}
public static HtmlNodeCollection GetElements(string html, string selector)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
return htmlDoc.DocumentNode.SelectNodes(selector);
}
public static string RemoveElements(string html, string selector)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var nodesToRemove = htmlDoc.DocumentNode.SelectNodes(selector);
if (nodesToRemove != null)
{
foreach (var node in nodesToRemove)
{
node.Remove();
}
}
return htmlDoc.DocumentNode.OuterHtml;
}
public static string RemoveAttribute(string html, string attrName)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
// Select all elements with a style attribute
var nodesWithStyle = htmlDoc.DocumentNode.SelectNodes($"//*[@{attrName}]");
if (nodesWithStyle != null)
{
foreach (var node in nodesWithStyle)
{
node.Attributes.Remove(attrName);
}
}
return htmlDoc.DocumentNode.OuterHtml;
}
}