1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I click on this drop down box and select a value programmically? Code included

Discussion in 'Visual Basic .NET' started by simpleonline1234, May 16, 2011.

  1. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    169
    Likes Received:
    13
    Code:
    <select id="questions" name="selection" >
      <option value="choosequestion" style="font-style:italic;">
      Choose a question ...
      </option>
      <option value="What is the name of your best friend from childhood?"
          
        >What is the name of your best friend from childhood?</option>
      <option value="What was the name of your first teacher?"
          
        >What was the name of your first teacher?</option>
      <option value="What is the name of your manager at your first job?"
          
        >What is the name of your manager at your first job?</option>
      <option value="What was your first phone number?"
          
        >What was your first phone number?</option>
      <option value="What is your vehicle registration number?"
          
        >What is your vehicle registration number?</option>
      <option value="What is your library card number?"

    I haven't been able to grab an elements an auto select it from the page. Got any ideas on how to go about getting this to work?
    Thanks.

    Here's something that I've slapped gather and it does do anything


    Code:
       For
     Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("select")(0).GetElementsByTagName("option")
                If element.InnerText = "What is the name of your best friend from childhood?" Then
                    element.SetAttribute("selected", "True")
                End If
            Next
     
  2. milans

    milans Regular Member

    Joined:
    Mar 26, 2011
    Messages:
    370
    Likes Received:
    149
    I think I can do this easily with jQuery but I don't know if you want to use this framework?
     
  3. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    The items of the cb need to be accessed by index. Also, this loop is making my head 'looping' :)

    You are right to learn to do it manually at first but in this case we have an ID value which should be unique in a correctly built page. So the control can be addressed directly:
    Code:
     
            Dim myElement As HtmlElement
            myElement = WebBrowser1.Document.GetElementById("questions")
            myElement.SetAttribute("selectedIndex", "3")
    
     
  4. shudogg

    shudogg Regular Member

    Joined:
    Sep 23, 2008
    Messages:
    412
    Likes Received:
    153
    Occupation:
    Internet Marketing
    Location:
    Indiana
    Home Page:
    You could do it this way as well:

    Code:
    WebBrowser1.Document.GetElementById("questions").SetAttribute("value", "What was your first phone number?")
    
    You don't need to loop or anything.. just this 1 line of code.