2

In C#, I managed to get the entire HTMLDocumentClass from an InternetExplorer object (navigating to a certain URL).

However, in Visual Studio 2008's debug mode, the content of this HTMLDocumentClass for this particular URL is MASSIVE, including attributes like activeElement, alinkColor, all, applets, charset, childNodes, etc, etc ,etc.

There's a button in that page that I want the to change to "Clicked". But I have no idea how to find the name/id/tag of that button. There's a simple tutorial that uses statements like :

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

But the structure of my URL is 100 times more complex than that.

Let's say the URL is yahoo.com, and I want to 'click' the Web Search button.

Any systematic way of going about this?

1 Answer 1

5

This is assuming my WebBrowser control is at Yahoo. The search button's id is "searchsubmit"

Using Windows.Forms.HtmlDocument

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

If using mshtml and 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();

If you look at Yahoo source, you will see that the search textbox is within multiple divs. htmlDoc.all.item takes care of it.

2
  • Ok. what if this button is buried within three layers of DIV elements. That is, it is under DIV3, which is under DIV2, which is under DIV1. how can i navigate? Assuming mshtml
    – Saobi
    Jul 15, 2009 at 21:19
  • I updated my answer. It doesn't matter how many DIV's there, htmlDoc.all.item goes through all of the items in the document.
    – Stan R.
    Jul 15, 2009 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.