views:

2564

answers:

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  A: 

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.

Stan R.
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
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.
did this work for you?
Stan R.
This worked awesome for me Stan! Thanks for posting it.
Chrisb
@Chrisb, no problem
Stan R.
A: 

hi have a question: i want to click a button that has no name or id. this is the html input:

how can i use: HTMLInputElement Button = (HTMLInputElement)htmlDoc.all.item("???", 0);

can i use the value, to click the button?? if yes, please could you show me how??