Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / C#

Efficient data entry through browser automation

Rate me:
Please Sign up or sign in to vote.
4.93/5 (72 votes)
29 Feb 2012Ms-PL7 min read 117.4K   1.7K   57  
You will learn how to create a semi automated crawler, and automate browsing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NCrawler
{
	public abstract class Selector
	{
		protected abstract IEnumerable<HtmlElement> SelectCore(HtmlDocument document);

		public IEnumerable<HtmlElement> Select(HtmlDocument document)
		{
			var elements = SelectCore(document);
			if(selectChildren != null)
				return elements.SelectMany(e => e.Children.OfType<HtmlElement>()).Where(selectChildren);
			return elements;
		}

		Func<HtmlElement, bool> selectChildren;
		public Selector SelectChildren(Func<HtmlElement, bool> predicate)
		{
			selectChildren = predicate;
			return this;
		}

		public void ForEach(HtmlDocument document, Action<HtmlElement> action)
		{
			foreach(var element in Select(document))
				action(element);
		}


		public static implicit operator Selector(string id)
		{
			return new IdSelector(id);
		}
	}

	public class IdSelector : Selector
	{
		public IdSelector(string id)
		{
			_Id = id;
		}
		private readonly string _Id;
		public string Id
		{
			get
			{
				return _Id;
			}
		}


		protected override IEnumerable<HtmlElement> SelectCore(HtmlDocument document)
		{
			yield return document.GetElementById(Id);
		}
	}
	public class ClassSelector : Selector
	{
		public ClassSelector(string className)
		{
			_ClassName = className;
		}
		private readonly string _ClassName;
		public string ClassName
		{
			get
			{
				return _ClassName;
			}
		}

		protected override IEnumerable<HtmlElement> SelectCore(HtmlDocument document)
		{
			foreach(HtmlElement element in document.All)
			{
				dynamic dom = element.DomElement;
				string className = dom.className;
				if(className == ClassName)
					yield return element;
			}
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions