Automatic Web Page Use With .NET
Written by Ian Elliot   
Wednesday, 28 August 2013
Article Index
Automatic Web Page Use With .NET
Accessing the data
Complete Program Listing

The Complete Program

This program isn't included in the codebin because it is very specific to the website it targets and therefore can't actually be run. 

The full listing of the program as described is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace NetUse
{
 public partial class Form1 : Form
 {
  private int page;
  private WebBrowser webBrowser1;
  public Form1()
  {
   InitializeComponent();

  }

  private void makeBrowser()
  {
   webBrowser1 = new System.Windows.
                       Forms.WebBrowser();
   webBrowser1.Name = "webBrowser1";
  }

 

  private void showBrowserInForm()
  {
   webBrowser1.Location = new    
           System.Drawing.Point(12, 300);
   webBrowser1.MinimumSize = new
           System.Drawing.Size(20, 20);
   webBrowser1.Size = new
           System.Drawing.Size(783, 338);
   webBrowser1.TabIndex = 1;
   Controls.Add(webBrowser1);
  }

  private void button1_Click(
           object sender,
           EventArgs e)

  {
   page = 1;
   makeBrowser();
   showBrowserInForm();
   webBrowser1.DocumentCompleted += new    
    WebBrowserDocumentCompletedEventHandler
                      (DocumentCompleted);
   webBrowser1.Navigate("login URL");
  }
 

  private void login()
  {
   HtmlElement name = webBrowser1.Document.
             GetElementById("txtUserName");
   if (name != null)
   {
     name.InnerText = "User name";
   }

   HtmlElement password =webBrowser1.
     Document.GetElementById("pasPassword");
   if (password != null)
   {
     password.InnerText = "password";
   }
   HtmlElement submit = webBrowser1.
    Document.GetElementById("login-submit");
   if (submit != null)
   {
    submit.InvokeMember("click");
   }

  }


  private void gotoData()
  {
   webBrowser1.Navigate("URL of Data Page");
  }


  private void getData()
  {
   string text = webBrowser1.DocumentText;
   Regex ex1 = new
                Regex(@"used\s+(\d+.\d+)");
   string used = ex1.Match(text).
                         Groups[1].ToString();
   textBox1.Text = used;
   webBrowser1.Dispose();
  }

 

  void DocumentCompleted(
           object sender,    
    WebBrowserDocumentCompletedEventArgs e)
  {
   if ( webBrowser1.ReadyState!=
     WebBrowserReadyState.Complete) return;
   switch (page)
   {
    case 1:
     page = 2;
     login();
     break;
    case 2:
     page = 3;
     gotoData();
     break;
    case 3:
     page = 4;
     getData();
     break;
   }

  }
 }
}

 

Related Articles:
Hit Highlighting with dtSearch
Automating applications using messages

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+Twitter, Linkedin or Facebook or sign up for our weekly newsletter.

 

Banner


A Customisable Weather Forecast

Having an accurate weather forecast is critical for many situations, in particular for deciding weather conditions are suitable for to deploy infrastructure inspection drones. This [ ... ]



SNTP Time Class

SNTP is a network protocol for obtaining an accurate time and it is an interesting exercise to build an SNTP client. In this article the language used is C# but it is easy enough to generalise to a la [ ... ]


Other Projects

 

 

 



Last Updated ( Thursday, 16 January 2014 )