C#を使用してWinFormアプリケーション内にWebページを読み込んでいます
そのページの特定のフィールドに(WATINを使用せずに)プログラムでデータを入力する必要があります。
誰かが他の解決策を持っているなら、私はそれを受け入れます。
問題のページにはAJAXまたはJavaScriptがありません。シンプルなHTMLデータ入力フォームです。
C#を使用してWinFormアプリケーション内にWebページを読み込んでいます
そのページの特定のフィールドに(WATINを使用せずに)プログラムでデータを入力する必要があります。
誰かが他の解決策を持っているなら、私はそれを受け入れます。
問題のページにはAJAXまたはJavaScriptがありません。シンプルなHTMLデータ入力フォームです。
コントロール のDocument
プロパティを使用してこれを行うことができます。WebBrowser
C# コード:
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
WebBrowser
コントロールに読み込まれたデモ HTML ページ:
<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>
WebClientを使用 してページをダウンロードし、HtmlAgilityPackを使用して解析します。
例:
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;
}
WinForm アプリの WebBrowser コントロールに Web ページを読み込んでいると仮定すると、WebBrowser.HtmlDocument.DomDocument プロパティを介してドキュメントにアクセスできるはずです。これは、MSHTML.IHTMLDocument2 インターフェイスを介したページの IE DOM へのアンマネージ リファレンスです。
MSHTML.Dll および SHDocVw.dll を使用する
ボタンデータをクリックするだけでWebページに転送されますが、WebページでもあなたのHtmlページでも同じようにコントロールします
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;
}
}
}
}
}
好きなように変更してください。うまくいくと確信しています。