using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DOMGetElementsSample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Browser browser = BrowserFactory.Create())
            {
                ManualResetEvent waitEvent = new ManualResetEvent(false);
                browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
                {
                    // Wait until main document of the web page is loaded completely.
                    if (e.IsMainFrame)
                    {
                        DOMDocument document = e.Browser.GetDocument();
                        List<DOMNode> divs = document.GetElementsByTagName("div");
                        foreach (DOMNode div in divs)
                        {
                            if (div.NodeType == DOMNodeType.ElementNode)
                            {
                                DOMElement divElement = (DOMElement)div;
                                Console.Out.WriteLine("class = " +
                                        divElement.GetAttribute("class"));
                            }
                        }
                        waitEvent.Set();
                    }
                };
                browser.LoadURL("http://www.google.com");
                waitEvent.WaitOne();
            }
        }
    }
}