Bartosz
Bartosz

Reputation: 4766

Access and use select element on a webpage

I am having troubles with using a select element on a website with my vbscript. The whole select element is designed to generate a list of users in one box, the users clicks and selects one or more users and presses ADD button so that these users are transferred to the second box. Essentially, one of them is a list of "available users", the second one is "users in use".

This is the structure of the select element list.

<option value="009">
    John Doe
</option>
<option value="278"> … </option>
<option value="556"> … </option>

I know that I can select the Element by objDivContent.document.GetElementById("available_members").value=009, but the problem here is that I don't know the values of these elements. The values are more or less static, but I have no access to them - I can have a list of the names, i.e. the content between the tags.

What I need my script to do, is to feed it with a list of names and load the page, make it select these users and press "add" button for each of them. Or for all at once, since it's a multiple choice.

Any Idea how can I access these elements having the data I do?

Upvotes: 0

Views: 88

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Load your user list into a dictionary, e.g. like this:

Set namelist = CreateObject("Scripting.Dictionary")
namelist.CompareMode = vbTextCompare  'case-insensitive

For Each name In WScript.Arguments.Unnamed
  namelist(name) = True
Next

With that you should be able to add names matching entries from the dropdown list like this:

Set memberlist = objDivContent.document.getElementById("available_members")
Set addButton  = objDivContent.document.getElementById("add_button")

For Each opt In memberlist.options
  If namelist.Exists(opt.text) Then
    opt.Selected = True
    addButton.Click()
    opt.Selected = False
  End If
Next

Upvotes: 1

Related Questions