Click here to Skip to main content
14,301,387 members
Rate this:
Please Sign up or sign in to vote.
See more:
HI Experts,

I have developed a windows form application. Using this application I am opening a website. This website used to search user's data on the basis of user's last name and birth date. My application is automatically opening the website. placing the user's last name and birth date to its corresponding textboxes. After this clicking the search button and getting the result.

Problem: After getting the result in the webpage I have to read some data from the page. This result is coming in div. so how to get that div content.

Any help will be appreciated..!

What I have tried:

<pre>using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using mshtml;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Net;

namespace mshtml_automation_demo
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
		private AxSHDocVw.AxWebBrowser axWebBrowser1;
		private int Task = 1; // global

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
			// Required for Windows Form Designer support

			// TODO: Add any constructor code after InitializeComponent call

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
			if( disposing )
				if (components != null) 
			base.Dispose( disposing );

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
			// axWebBrowser1
			this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.axWebBrowser1.Enabled = true;
			this.axWebBrowser1.Location = new System.Drawing.Point(0, 0);
			this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
			this.axWebBrowser1.Size = new System.Drawing.Size(616, 382);
			this.axWebBrowser1.TabIndex = 0;
			this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);
			// MainForm
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 382);
			this.Name = "MainForm";
			this.Text = "Microsoft WebBrowser Automation";
			this.Load += new System.EventHandler(this.FrmMain_Load);


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main() 
			Application.Run(new MainForm());

		private void FrmMain_Load(object sender, System.EventArgs e)
            object loc = "";
			object null_obj_str = "";
			System.Object null_obj = 0;
			this.axWebBrowser1.Navigate2(ref loc , ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
        public static void OutputText(string VcaTypeExpDate)
            string output = "C:\\Export\\Temp\\Logs\\";
            //string output = "C:\\Export\\Raildocs\\Logs\\";
            // string output = "F:\\Test_Cord\\Log\\";
            StreamWriter Res;
            FileStream fileStream = null;
            DirectoryInfo logDirInfo = null;
            FileInfo logFileInfo;
            var ResFilePath = output;
            ResFilePath = ResFilePath + "Result.txt";
            logFileInfo = new FileInfo(ResFilePath);
            logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
            if (!logDirInfo.Exists) logDirInfo.Create();
            if (!logFileInfo.Exists)
                fileStream = logFileInfo.Create();
                fileStream = new FileStream(ResFilePath, FileMode.Append);
            Res = new StreamWriter(fileStream);

		private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
            List<string> CVList = new List<string>();
            string inputCV = ConfigurationManager.AppSettings["inputFile"];
            string outputCV = ConfigurationManager.AppSettings["outputFile"];
            string[] lines = File.ReadAllLines(inputCV, Encoding.UTF8);
            foreach (var item in lines)
                string CVData = item.ToString().Replace('@', ' ');

            foreach(var item in CVList)
                string[] cvData = item.Split(',');
				case 1:

					HTMLDocument myDoc = new HTMLDocumentClass();
					myDoc = (HTMLDocument) axWebBrowser1.Document;

					// a quick look at the google html source reveals: 
					// <INPUT maxLength="256" size="55" name="q">
                    HTMLInputElement otxtSearchBox = (HTMLInputElement)myDoc.all.item("ctl00_ContentPlaceHolder1_ctl00_txtAchternaam", 0);

                    otxtSearchBox.value = cvData[0].ToString();

                    HTMLInputElement dtxtSearchBox = (HTMLInputElement)myDoc.all.item("ctl00_ContentPlaceHolder1_ctl00_txtGeboortedatum", 0);

                     dtxtSearchBox.value = cvData[1].ToString();

					// google html source for the I'm Feeling Lucky Button:
					// <INPUT type=submit value="I'm Feeling Lucky" name=btnI>
                    HTMLInputElement btnSearch = (HTMLInputElement)myDoc.all.item("ctl00_ContentPlaceHolder1_ctl00_cmdOpvragen", 0);;


                        // write to text file 


				case 2:

					// continuation of automated tasks...
            MessageBox.Show("Operation Completed...!");
Updated 4-Apr-17 4:05am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Use the HTMLDocument you can read all functions you need on: htmldocument msdn

You can use


So you need to know a Element by Id for getting the result. If you don't know the element ide you searching for simple use the string and find/replace function in c# and apply with mydoc.Write(String). You can apply changes with

string urlAddress = "";
var doc = new HtmlDocument();

using (WebClient client = new WebClient())
    string htmlCode = client.DownloadString(urlAddress);

//do whatever you want with the doc

Also if you want a more stable solution you need to checkout this htmlAgile project: htmlagilitypack it got more options. But to complex to just write down here, you should definitely check it out.
Mukesh Pr@sad 5-Apr-17 5:12am
Its giving null value ...not the exact value what I want.
after button click I am getting the result that result I need to grab.
But the code what u have suggested is still checking on the empty page I mean befor button click not after button click.
Wessel Beulink 5-Apr-17 5:28am
I updated the solution for you, hope this will work for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100