Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top