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
			//
			InitializeComponent();

			//
			// 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) 
				{
					components.Dispose();
				}
			}
			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();
			((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
			this.SuspendLayout();
			// 
			// 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.Controls.Add(this.axWebBrowser1);
			this.Name = "MainForm";
			this.Text = "Microsoft WebBrowser Automation";
			this.Load += new System.EventHandler(this.FrmMain_Load);
			((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

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

		private void FrmMain_Load(object sender, System.EventArgs e)
		{
            object loc = "https://www.vca.ssvv.nl/";
			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();
            }
            else
            {
                fileStream = new FileStream(ResFilePath, FileMode.Append);
            }
            Res = new StreamWriter(fileStream);
            Res.WriteLine(VcaTypeExpDate);
            Res.Close();
        }

		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('@', ' ');
                CVList.Add(CVData);

            }
            foreach(var item in CVList)
            {
                string[] cvData = item.Split(',');
                switch(Task)
			{
				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);
					btnSearch.click();
                   



					Task++;

                        // write to text file 


					break;

				case 2:

					// continuation of automated tasks...
					break;
			}
            }
            MessageBox.Show("Operation Completed...!");
			
		}
	}
}
Posted
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
mydoc.GetElementById(String)

Or
mydoc.GetElementsByTagName(String)


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
mydoc.CreateElement(String)
mydoc.Write(String)



try:
string urlAddress = "http://google.com.ssvv.nl/";
var doc = new HtmlDocument();

using (WebClient client = new WebClient())
{
    string htmlCode = client.DownloadString(urlAddress);
    doc.LoadHtml(htmlCode);
}


//do whatever you want with the doc
doc.GetElementById('youId')


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.
   
v3
Comments
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