質問

C# では、(特定の URL に移動して) InternetExplorer オブジェクトから HTMLDocumentClass 全体を取得することができました。

ただし、Visual Studio 2008 のデバッグ モードでは、この特定の URL の HTMLDocumentClass のコンテンツは大量であり、activeElement、alinkColor、all、アプレット、charset、childNodes などの属性が含まれます。

そのページには「クリック済み」に変更したいボタンがあります。しかし、そのボタンの名前/ID/タグを見つける方法がわかりません。次のようなステートメントを使用する簡単なチュートリアルがあります。

HTMLInputElement button =
  (HTMLInputElement)theDoc.getElementById("Button1");
button.click();

しかし、私の URL の構造はそれよりも 100 倍複雑です。

URL が yahoo.com で、Web 検索ボタンを「クリック」したいとします。

これに対処するための体系的な方法はありますか?

役に立ちましたか?

解決

これは、WebBrowser コントロールが Yahoo にあることを前提としています。検索ボタンのIDは「searchsubmit」です

Windows.Forms.HtmlDocument の使用

 HtmlElement button = (HtmlElement)htmlDoc.GetElementById("searchsubmit");
 button.InvokeMember("click");

mshtml と HTMLInputElement を使用する場合

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

   //find the search text box..
   HTMLInputElement searchTextBox = (HTMLInputElement)htmlDoc.all.item("p", 0);
   searchTextBox.value = "Stack Overflow";

   //find the button
   HTMLInputElement searchButton = (HTMLInputElement)htmlDoc.all.item("searchsubmit", 0);
   searchButton.click();

Yahoo のソースを見ると、検索テキストボックスが複数の div 内にあることがわかります。 htmlDoc.all.item それを大事にしてくれます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top