Reputation: 9541
I have a webpage loading inside my WinForm application using C#
I need to programatically type data into specific fields on that page (without using WATIN).
If anyone has any other solution, I am open to it.
The page in question has NO AJAX or JavaScript. They are simple HTML data entry forms.
Upvotes: 0
Views: 11406
Reputation: 2857
Use the MSHTML.Dll and SHDocVw.dll
I am just paste code that transfre the code from winform to IE browser in which you just click on button data is transfer to web page but controls as same on web page as well on yours Html page
private SHDocVw.InternetExplorer TargetIE = null;
string url;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
GetTheIEObjectFromSystem);
SendTextToActiveElementWithSubmitOptionSet();
}
private void GetTheIEObjectFromSystem(
{
SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer internetExplorer in SWs)
{
url = internetExplorer.LocationURL;
TargetIE = internetExplorer;
return;
}
}
private void SendTextToActiveElementWithSubmitOptionSet()
{
mshtml.IHTMLDocument2 document = null;
document = TargetIE.Document as mshtml.IHTMLDocument2;
if (!document.activeElement.isTextEdit)
{
MessageBox.Show("Active element is not a text-input system");
}
else
{
HTMLInputElement HTMLI;
HTMLI = document.activeElement as HTMLInputElement;
var tag = HTMLI.document as mshtml.HTMLDocumentClass;
mshtml.IHTMLElementCollection hTMLElementCollection = tag.getElementsByTagName("input");
//for (int i = 0; i < a.length; i++)
{
foreach (mshtml.HTMLInputElement el in hTMLElementCollection)
{
switch (el.id)
{
case "txtFirstName":
el.value = textBox1.Text;
break;
case "txtLastName":
el.value = textBox2.Text;
break;
case "txtAddress":
el.value = textBox3.Text;
break;
case "txtMobile":
el.value = textBox4.Text;
break;
}
}
}
}
}
You make changes as you want I am sure it works
Upvotes: 1
Reputation: 37788
You can do so using the Document
property of the WebBrowser
control :
C# code:
if (webBrowser1.Document == null) return;
var form = webBrowser1.Document.Forms[0]; //form element
var input = form.Children[0]; //input element
input.SetAttribute("value","input value"); //set the input value
form.InvokeMember("submit"); //submit the form
Demo HTML page loaded into the WebBrowser
Control:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="">
<input type="text" name="testInput" value="test"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
Upvotes: 4
Reputation: 116098
Use WebClient to download the page and use HtmlAgilityPack to parse it.
An example:
using (var wc = new WebClient())
{
var page = wc.DownloadString(url);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
//XPath
var title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
var text = doc.DocumentNode.SelectSingleNode("//div[@id='readInner']")
.InnerText;
//Linq
var text = doc.DocumentNode.Descendants("div")
.Where(n => n.Attributes["id"].Value == "readInner")
.First()
.InnerText;
}
Upvotes: 3
Reputation: 10184
Assuming you are loading the web page into a WebBrowser control on your WinForm app, you should be able to access the document via the WebBrowser.HtmlDocument.DomDocument property. This is an unmanaged reference to the IE DOM for the page through the MSHTML.IHTMLDocument2 interface.
Upvotes: 3