using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TestCSharpApp
{
public partial class Form1 : Form
{
private bool CanExecute; //Checks for navigation later
private List<string> ListOfElems = new List<string>();
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("https://csharpforums.net/"); //Navigate first to the page with the values you want
}
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) //Has doc completed?
{
CanExecute = true;
}
private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) //Are we about to navigate?
{
CanExecute = false;
}
private string RunSearch(string HtmlClass)
{
var Element = webBrowser1.Document.GetElementsByTagName("a"); //a is the attribute of links
foreach (HtmlElement Meta in Element) //Iterate and search
{
if (Meta.GetAttribute("className") == HtmlClass.PadRight(1)) //Check if the class name exists in the page you're searching
//I used PadRight(1) to add spacing that is required for this example to retrieve HtmlClass passed into the function : "menu-linkRow u-indentDepth0 js-offCanvasCopy "
//Do not change "className", instead pass in your html class element.
{
var result = Meta.InnerText;
if (result.Contains("Current visitors")) //Check if it contains the value you are looking for
return result; //Or return an individual result
}
}
return string.Empty; //No matching element to return
}
private void Button1_Click(object sender, EventArgs e)
{
if (CanExecute == true) //Check if page completed navigating first
{
var retValue = RunSearch("menu-linkRow u-indentDepth0 js-offCanvasCopy "); //Run the function
if (retValue != string.Empty) //Check that the value was found
{
Console.WriteLine(retValue); //Do as you please with your value
}
}
else
MessageBox.Show("Wait for the page to finish navigating first.");
}
}
}