C# で IE を操作する
SMBCフレンド証券の買い付け画面用。
指値・成行のドロップダウンリストボックスと、当日・出合のラジオボタンを、操作できるようにした。
using System.Windows.Forms; using System; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { wb.Navigate("https://online.smbc-friend.co.jp/smbcfs-front/loginInformation.do"); } private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { HtmlDocument doc = wb.Document; HtmlWindow win = doc.Window; // System.Windows.Forms.HtmlWindow HtmlWindowCollection frames = win.Frames; // System.Windows.Forms.HtmlWindowCollection if (frames.Count == 0) { HtmlElement loginForm = doc.Forms["loginForm"]; // System.Windows.Forms.HtmlElement if (loginForm != null) { HtmlElementCollection inputElements = loginForm.GetElementsByTagName("input"); // input タグ HtmlElement username = inputElements["username"]; // お客様コード // String value = username.GetAttribute("value"); username.SetAttribute("value", textBox1.Text); HtmlElement password = inputElements["password"]; // 暗証番号 password.SetAttribute("value", textBox2.Text); } } else if (frames.Count == 1) { } else if (frames.Count == 2) { HtmlWindow headFrame = frames["Head"]; HtmlWindow mainFrame = frames["Main"]; HtmlWindow leftFrame = mainFrame.Frames["Left"]; HtmlWindow rightFrame = mainFrame.Frames["Right"]; // "株式 買付のご注文" の画面か検査する。 // 同じフォーム名で、銘柄入力(fromMeigara=false)とその次の株数・値段等入力(fromMeigara=true)の画面がある。 HtmlElement stockBuyAgreementForm = rightFrame.Document.Forms["stockBuyAgreementForm"]; if (stockBuyAgreementForm != null) { HtmlElementCollection inputElements = stockBuyAgreementForm.GetElementsByTagName("input"); // input タグ HtmlElement fromMeigara = inputElements["fromMeigara"]; String valueFromMeigara = fromMeigara.GetAttribute("value"); if (valueFromMeigara == "false") { // なにもしない。 } else if (valueFromMeigara == "true") { HtmlElement primaryMarketCode = inputElements["primaryMarketCode"]; String valuePrimaryMarketCode = primaryMarketCode.GetAttribute("value"); if (valuePrimaryMarketCode == "") { // 銘柄コードが正しくなかった時に出るエラー画面だった。 // なにもしない。 } else { // 執行条件(指値・成行)のドロップダウンリストボックス HtmlElementCollection selectElements = stockBuyAgreementForm.GetElementsByTagName("select"); // select タグ HtmlElement orderExecCond = selectElements["orderExecCond"]; HtmlElementCollection optionElements = orderExecCond.GetElementsByTagName("option"); HtmlElement op1th = optionElements[1]; // 指値(value="0") op1th.SetAttribute("selected", "true"); // 注文期限(当日・出合(週中))のラジオボタン HtmlElementCollection terms = inputElements.GetElementsByName("term"); HtmlElement term0th = terms[0]; // 当日(value="9") term0th.SetAttribute("checked", "true"); } } else { Console.WriteLine("aaa"); throw new Exception(); } } } else { } } private void textBox1_TextChanged(object sender, EventArgs e) { } } }
input type="text" や input type="radio" や select は操作できるようになったのだが、form の submit ってどうやればいいの?
http://dobon.net/cgi-bin/vbbbs/cbbs.cgi?mode=al2&mo=21397&namber=21391&space=15&rev=0&page=0&no=0
を見ると、submit ボタンを click しないといけないのかな?ボタンを click って余計なような気がするが、しないといけないのか?
stockBuyAgreementForm.InvokeMember("submit");
みたいなのだけでも良いのかな?
しかし、デバッグしづらい。VB だと、ブレークポイントで止めていても、操作対象である IE の画面を見ることができるのだが、C# だと、画面が見れない。Collection の要素をウォッチウィンドウで見る方法も良く分からない。