The complete sample solution can be found in the attachments to this article. The project in this solution has NuGet dependencies, which will be resolved automatically during build.
C#
using DotNetBrowser; using DotNetBrowser.DOM; using DotNetBrowser.DOM.Events; using DotNetBrowser.Events; using DotNetBrowser.WinForms; using DotNetBrowser.WPF; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Extract_search_results_to_text_file_from_Google { public partial class Form1 : Form { public Form1() { InitializeComponent(); //Change address bar by event OnClick DOMEventHandler domEvent = delegate (object sender1, DOMEventArgs e1) { DOMEventType eventType = e1.Type; }; browserView.Browser.FinishLoadingFrameEvent += delegate (object sender2, FinishLoadingEventArgs e2) { if (e2.IsMainFrame) { DOMDocument document = e2.Browser.GetDocument(); DOMElement documentElement = document.DocumentElement; documentElement.AddEventListener(DOMEventType.OnClick, domEvent, false); } try { //toolStripDebug.Text = browserView.Browser.GetDocument().GetElementByClassName("cdr_frm").GetElementByTagName("input").GetAttribute("value").ToString(); toolStripAddress.Text = browserView.Browser.URL.ToString(); this.Text = browserView.Browser.Title; } catch { } }; } private void Form1_Load(object sender, EventArgs e) { browserView.Browser.LoadURL("http://www.google.com"); //ComplexPageLoad(); this.Text = browserView.Browser.Title; toolStripAddress.Text = browserView.Browser.URL.ToString(); } private void toolStripAddress_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { ComplexPageLoad(); this.Text = browserView.Browser.Title; toolStripAddress.Text = browserView.Browser.URL.ToString(); } } private void ComplexPageLoad() { ManualResetEvent resetEvent = new ManualResetEvent(false); FinishLoadingFrameHandler listener = new FinishLoadingFrameHandler((object sender, FinishLoadingEventArgs e) => { if (e.IsMainFrame) { resetEvent.Set(); } }); browserView.Browser.FinishLoadingFrameEvent += listener; try { browserView.Browser.LoadURL(toolStripAddress.Text.ToString()); resetEvent.WaitOne(new TimeSpan(0, 0, 45)); } finally { browserView.Browser.FinishLoadingFrameEvent -= listener; } } private void toolStripGoogleSearchToFile_Click(object sender, EventArgs e) { //Only for Google search results DOMDocument document = browserView.Browser.GetDocument(); string allTextToFile = ""; string searchWord = ""; try { searchWord = document.GetElementByClassName("q qs").GetAttribute("href").Split('&')[0].ToString(); } catch { MessageBox.Show("There is nothing to parse", "Error!"); } if (searchWord.Contains("/search?q=")) { ManualResetEvent resetEvent = new ManualResetEvent(false); FinishLoadingFrameHandler listener = new FinishLoadingFrameHandler((object sender1, FinishLoadingEventArgs e1) => { if (e1.IsMainFrame) { resetEvent.Set(); } }); browserView.Browser.FinishLoadingFrameEvent += listener; try { browserView.Browser.LoadURL("https://www.google.com.ua" + searchWord); resetEvent.WaitOne(new TimeSpan(0, 0, 45)); } finally { browserView.Browser.FinishLoadingFrameEvent -= listener; } // Return address of Web page toolStripAddress.Text = browserView.Browser.URL.ToString(); this.Text = browserView.Browser.Title; int countDiv = document.GetElementsByTagName("div").Count; int countH3 = 0; //Quantity of <h3> tags in <div> tags for (int i = 0; i < countDiv; i++) { int tmpH3 = document.GetElementsByTagName("div")[i].GetElementsByTagName("h3").Count; countH3 += tmpH3; } //Search for <a> tags in <h3> for (int i = 0; i < countH3; i++) { string text = ""; try { text = document.GetElementsByTagName("h3")[i].GetElementByTagName("a").InnerText.ToString(); } catch { break; } //Make string for add info to file string textToFile = "# " + (i + 1) + Environment.NewLine + "name = " + text + Environment.NewLine + "hyperlink = " + document.GetElementsByTagName("h3")[i].GetElementByTagName("a").GetAttribute("href") + Environment.NewLine; allTextToFile += textToFile + Environment.NewLine; } SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; //Choose path and name to save the file if (saveFileDialog.ShowDialog() == DialogResult.OK) { //Format name of file string txtFileName = saveFileDialog.FileName.ToString(); //string weq = saveFileDialog. File.WriteAllText(txtFileName, allTextToFile); } } } } }
VB.NET
Imports System.IO Imports System.Threading Imports DotNetBrowser Imports DotNetBrowser.DOM Imports DotNetBrowser.DOM.Events Imports DotNetBrowser.Events Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() Dim domEvent As DOMEventHandler = sub(ByVal sender1 As Object, ByVal e1 As DOMEventArgs) Dim eventType As DOMEventType = e1.Type End sub AddHandler browserView.Browser.FinishLoadingFrameEvent, sub(ByVal sender2 As Object, ByVal e2 As FinishLoadingEventArgs) If e2.IsMainFrame Then Dim document As DOMDocument = e2.Browser.GetDocument() Dim documentElement As DOMElement = document.DocumentElement documentElement.AddEventListener(DOMEventType.OnClick, domEvent, False) End If Try toolStripAddress.Text = browserView.Browser.URL.ToString() Me.Text = browserView.Browser.Title Catch End Try End sub End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) browserView.Browser.LoadURL("http://www.google.com") Me.Text = browserView.Browser.Title toolStripAddress.Text = browserView.Browser.URL.ToString() End Sub Private Sub toolStripAddress_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) If e.KeyCode = Keys.Enter Then ComplexPageLoad() Me.Text = browserView.Browser.Title toolStripAddress.Text = browserView.Browser.URL.ToString() End If End Sub Private Sub ComplexPageLoad() Dim resetEvent As ManualResetEvent = New ManualResetEvent(False) Dim listener As FinishLoadingFrameHandler = New FinishLoadingFrameHandler(sub(ByVal sender As Object, ByVal e As FinishLoadingEventArgs) If e.IsMainFrame Then resetEvent.[Set]() End If End sub) AddHandler browserView.Browser.FinishLoadingFrameEvent, listener Try browserView.Browser.LoadURL(toolStripAddress.Text.ToString()) resetEvent.WaitOne(New TimeSpan(0, 0, 45)) Finally RemoveHandler browserView.Browser.FinishLoadingFrameEvent, listener End Try End Sub Private Sub toolStripGoogleSearchToFile_Click(ByVal sender As Object, ByVal e As EventArgs) Dim document As DOMDocument = browserView.Browser.GetDocument() Dim allTextToFile As String = "" Dim searchWord As String = "" Try searchWord = document.GetElementByClassName("q qs").GetAttribute("href").Split("&"c)(0).ToString() Catch MessageBox.Show("There is nothing to parse", "Error!") End Try If searchWord.Contains("/search?q=") Then Dim resetEvent As ManualResetEvent = New ManualResetEvent(False) Dim listener As FinishLoadingFrameHandler = New FinishLoadingFrameHandler(sub(ByVal sender1 As Object, ByVal e1 As FinishLoadingEventArgs) If e1.IsMainFrame Then resetEvent.Set() End If End sub) AddHandler browserView.Browser.FinishLoadingFrameEvent, listener Try browserView.Browser.LoadURL("https://www.google.com.ua" & searchWord) resetEvent.WaitOne(New TimeSpan(0, 0, 45)) Finally RemoveHandler browserView.Browser.FinishLoadingFrameEvent, listener End Try toolStripAddress.Text = browserView.Browser.URL.ToString() Me.Text = browserView.Browser.Title Dim countDiv As Integer = document.GetElementsByTagName("div").Count Dim countH3 As Integer = 0 For i As Integer = 0 To countDiv - 1 Dim tmpH3 As Integer = document.GetElementsByTagName("div")(i).GetElementsByTagName("h3").Count countH3 += tmpH3 Next For i As Integer = 0 To countH3 - 1 Dim text As String = "" Try text = document.GetElementsByTagName("h3")(i).GetElementByTagName("a").InnerText.ToString() Catch Exit For End Try Dim textToFile As String = "# " & (i + 1) & Environment.NewLine & "name = " & text & Environment.NewLine & "hyperlink = " & document.GetElementsByTagName("h3")(i).GetElementByTagName("a").GetAttribute("href") & Environment.NewLine allTextToFile += textToFile & Environment.NewLine Next Dim saveFileDialog As SaveFileDialog = New SaveFileDialog() saveFileDialog.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*" saveFileDialog.FilterIndex = 1 saveFileDialog.RestoreDirectory = True If saveFileDialog.ShowDialog() = DialogResult.OK Then Dim txtFileName As String = saveFileDialog.FileName.ToString() File.WriteAllText(txtFileName, allTextToFile) End If End If End Sub End Class