GetElementByID - using webbrowser when ID is not unique on the page

CreditSoupTech
CreditSoupTech used Ask the Experts™
on
I'm trying to retrieve an element which is a dropdown (select/options) element).  Unfortunately the programmer used the same name for an image on the page as for the select object.  When I use getElementByID - I get the image - and thus can't get to the 2nd object with that name. Is there any way I can iterate through all the elements in the document to find both of them -  or to specify the type of control I want it to search for?  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mark StegglesWeb Developer
Top Expert 2006

Commented:
An id should be unique in the document. Rather than trying to find another way to get to the select you should fix the cause of this issue. You should change either the image or the select to a unique id.
  When you're screen scraping you don't have the luxury of making corrections on the source pages.
    Thanks anyway.  I think I found how to loop through the elements and that might allow me to get the right one.


 HtmlDocument doc = webBrowser1.Document;
                elems = doc.All.GetElementsByName("");

                if (elems != null && elems.Count > 0)
                    for (int i = 0; i < elems.Count; i++)
                    {

                        {
                            elem = elems[i];
                            if (elem.TagName.Equals("FORM"))
                            {
                                elem.InvokeMember("Submit");
                                break;
                            }
                        }
                    }

Mark StegglesWeb Developer
Top Expert 2006

Commented:
O sorry. I didnt know the situation.

i think you could also do it like this:

var selects = document.getElementsByTagName('select');
for(i=0;i<selects;i++) {
  if(selects[i].id == 'yourID') {
   do your stuff
}
}

Author

Commented:
Thanks.  That might work. I got the other solution working though so I quit playing with it.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial