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

Posted on 2008-11-11
Last Modified: 2013-12-17
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?  
Question by:CreditSoupTech
    LVL 30

    Expert Comment

    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.
    LVL 4

    Accepted Solution

      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"))

    LVL 30

    Expert Comment

    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
    LVL 4

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now