Results 1 to 19 of 19

Thread: web browser drop down boxes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    web browser drop down boxes

    Hi,

    I am trying to select one of the two options below from a dropdown box in webbrowser, I am unable to get it working,

    Code:
    <option data-user-code='af' value='93'  aria-label='Andrew'>Andrew(351)</option><option data-user-code='al' value='352'  aria-label='James'>James (353)</option>
    Here's my code:

    Code:
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    WebBrowser1.Document.GetElementById("user-code").SetAttribute("selectedindex", "351")
    end sub
    So, trying to select Andrew. It doesnt do anything at all and no errors,

    thanks

  2. #2
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: web browser drop down boxes

    try
    Code:
    WebBrowser1.Document.GetElementById("user-code").SetAttribute("Value", "Andrew") 'or Andrew (351), also make sure user-code is the select elements id
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: web browser drop down boxes

    Quote Originally Posted by bensonsearch View Post
    try
    Code:
    WebBrowser1.Document.GetElementById("user-code").SetAttribute("Value", "Andrew") 'or Andrew (351), also make sure user-code is the select elements id
    Thanks, I tried both variations and it just left the dropdown blank, I am sure the user-code is correct ID.

  4. #4
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: web browser drop down boxes

    .GetElementById("") does just that, get's an element by it's id... The different options in the dropdown list don't contain id's. ID's are just like the "value=" you see, only it would be "id=*".

    What is the <select **** part that comes before the options?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: web browser drop down boxes

    Quote Originally Posted by detlion1643 View Post
    .GetElementById("") does just that, get's an element by it's id... The different options in the dropdown list don't contain id's. ID's are just like the "value=" you see, only it would be "id=*".

    What is the <select **** part that comes before the options?
    Code:
    <select aria-invalid="false" id="user-code" name='user-code' data="user-code-drop-down">

  6. #6
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: web browser drop down boxes

    We need to get the "option" elements that are part of the "select" element. Easiest way is to get a collection of the "select"'s children elements:
    Code:
    Dim htmlCol As HtmlElementCollection = webBrowser1.Document.GetElementById("user-Code").Children
    I can't remember VB looping atm, but in C#:
    Code:
    for(int i = 0; i < htmlCol.Count; i++)
    Inside of the loop, you can now test the elements one at a time. At this point, you'd need to know what to test for. For example, let's test for data-user-code:
    Code:
    If htmlCol(i).GetAttribute("data-user-code") == "*" Then...
    where * is equal to whatever string to test for. You can use the other attributes of the option elements if needed. Once you find the correct one you'd like to select:
    Code:
    htmlCol(i).SetAttribute("selected","selected")
    should select it.

  7. #7
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: web browser drop down boxes

    This is the code to select an item on a drop down in the web browser
    'newurl is a commonly used name for the parent but you will probably need to change this value to something else for the site your using
    'where imghp is place the value of the option you want to select
    Code:
    WebBrowser1.Document.GetElementById("newurl").SetAttribute("value", "imghp")
    I just tested this personally and it does work

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: web browser drop down boxes

    Quote Originally Posted by Vexslasher View Post
    This is the code to select an item on a drop down in the web browser
    'newurl is a commonly used name for the parent but you will probably need to change this value to something else for the site your using
    'where imghp is place the value of the option you want to select
    Code:
    WebBrowser1.Document.GetElementById("newurl").SetAttribute("value", "imghp")
    I just tested this personally and it does work
    hmm still confused, the code I put above,

    so ID = User-code so that goes where newurl is, then "value", Andrew? etc?

  9. #9
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: web browser drop down boxes

    In the html source code here is what they will usually look like
    parent tag
    HTML Code:
    <select name="newurl"
    sub item tag
    HTML Code:
    <option value="imghp">
    If you put the wrong thing you wont get a error message but your code wont do anything.

    And you don't change the text where it says "value" you change the text where it says imghp to whatever the value is for your sub item Andrew.
    Last edited by Vexslasher; Apr 2nd, 2014 at 11:41 AM.

  10. #10
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: web browser drop down boxes

    FYI:
    parent tag
    <select name="newurl"
    That should be:
    Code:
    <select id="somethingUniquelyNamed" name="SomeNameHere"...
    The .ElementByID("*") refers to the id, not the name.
    If you put the wrong thing you wont get a error message but your code wont do anything.
    Not really so, in my testing, if using .ElementByID("*") and the element with that id does not exist in the document, you will get an exception.
    Last edited by detlion1643; Apr 2nd, 2014 at 11:41 AM.

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    17

    Re: web browser drop down boxes

    I think Vexslasher has written that well. you should enter 93 or 351 instead of imghp? you could probably also use "aria-label" instead of "value", and the point to their names.

  12. #12
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: web browser drop down boxes

    @detlion1643
    I said "usually" in other words the tag can be coding a bunch of different ways, so that tag is not always the same.

  13. #13
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: web browser drop down boxes

    Quote Originally Posted by Vexslasher View Post
    @detlion1643
    I said "usually" in other words the tag can be coding a bunch of different ways, so that tag is not always the same.
    I was just pointing out that the id attribute is the one that gets tested and not the name attribute when using .ElementById("*"). If the element doesn't have an id attribute, then you gotta find another way to get to that element then.

  14. #14
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: web browser drop down boxes

    Well the webpage I was using to test my code had it in that tag and it worked with that one.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: web browser drop down boxes

    Right so I tried:
    Code:
    Dim pageinput as htmlelementcollection in pageinput
    for each elem as html element in pageinput
    
    if elem.getattribute("id)" = "user-code" then
    elem.setattribute("aria-label", "Andrew")
    end if
    next
    Nothing happend...hmm

  16. #16
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: web browser drop down boxes

    Can you provide the website your trying to accomplish this on. It would make it much easier for me to provide you with a solution that will work without being modified.

  17. #17
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: web browser drop down boxes

    Back and forth, back and forth. Why not simply post the url and be done with it.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub SomeMethod()
    4.         Dim element = Me.WebBrowser1.Document.GetElementsByTagName("select") _
    5.                       .Cast(Of HtmlElement) _
    6.                       .FirstOrDefault(Function(el) el.GetAttribute("name") = "surely has a name?")
    7.  
    8.         If element IsNot Nothing Then
    9.             element.SetAttribute("value", "what ever")
    10.         End If
    11.     End Sub
    12. End Class

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: web browser drop down boxes

    Thanks all for help,

    realised, I was using "input" when defining the getelementsbytagname, as this is what the other text fields are, created a new one for select and worked perfectly.

    Thanks for your time, much appreciated.

  19. #19
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: web browser drop down boxes

    Cant see once above where you have used bytagname..... Always makes me wonder when members refuse to give out links.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width