Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
The following sample code demonstrates how to find the first element that matches a specified CSS selector(s) in the document.
C#
using DotNetBrowser; using DotNetBrowser.DOM; using DotNetBrowser.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DOMQuerySelectorSample { class Program { static void Main(string[] args) { Browser browser = BrowserFactory.Create(); browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e) { if (e.IsMainFrame) { DOMDocument document = e.Browser.GetDocument(); DOMElement documentElement = document.DocumentElement; // Get the div with id = "root". DOMNode divRoot = documentElement.QuerySelector("#root"); // Get all paragraphs. List<DOMNode> paragraphs = divRoot.QuerySelectorAll("p"); foreach (var paragraph in paragraphs) { Console.Out.WriteLine("paragraph.TextContent = " + paragraph.TextContent); } } }; browser.LoadHTML( "<html><body><div id='root'>" + "<p>paragraph1</p>" + "<p>paragraph2</p>" + "<p>paragraph3</p>" + "</div></body></html>"); ; } } }
VB.NET
Imports DotNetBrowser Imports DotNetBrowser.DOM Module Module1 <STAThread> Sub Main() Dim browser As Browser = BrowserFactory.Create() AddHandler browser.FinishLoadingFrameEvent, Sub(o, e) If e.IsMainFrame Then Dim document As DOMDocument = e.Browser.GetDocument() Dim documentElement As DOMElement = document.DocumentElement ' Get the div with id = "root". Dim divRoot As DOMNode = documentElement.QuerySelector("#root") ' Get all paragraphs. Dim paragraphs As List(Of DOMNode) = divRoot.QuerySelectorAll("p") For Each paragraph In paragraphs Console.WriteLine("paragraph.TextContent = " + paragraph.TextContent) Next End If End sub browser.LoadHTML( "<html><body><div id='root'>" + "<p>paragraph1</p>" + "<p>paragraph2</p>" + "<p>paragraph3</p>" + "</div></body></html>") End Sub End Module