Select from Drop down list in Internet Page

23izkool

New Member
Joined
Mar 4, 2011
Messages
19
Hi all,
PROBLEM
I am trying to select an option within a dropdown list in internet explorer but cannot seem to access the 'select' tagname with all syntax

SOURCE CODE OF INTERNET PAGE
Code:
<SELECT class=c23_normalText id=selectlist name=selectlist> <OPTION value=1>This is Option 1</OPTION> <OPTION value=2 selected>This is Option 2</OPTION></SELECT>
(here is actually '<' but did not put in as webpage does not display properly)select id = "selectlist" name = "selectlist" class = "c23_normalText">
<OPTION selected value="1">(here is actually '<' but did not put in as webpage does not display properly)This is Option 1 </OPTION>
<OPTION selected value="2">(here is actually '<' but did not put in as webpage does not display properly)This is Option 2 </OPTION>
</SELECT>







WHAT I TRIED SO FAR
I have tried the following syntax to select Option 1 but to no avail:
~ "With appIE
.document.forms.Item("selectlist").Value = "1"
(I keep getting the error "Runtime error '91'. Object variable or With block variable not set)
~ Also tried "getelementsbytagname("selectlist") but also could not access
Hope someone can help on this! Thanks!
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Sorry people that the code did not display properly. It should look like the bottom:

(This front symbol is "<")_select id = "selectlist" name = "selectlist" class = "c23_normalText">
(This front symbol is "<")_option value = "1" selected>This is Option 1 </option>
(This front symbol is "<")_option value = "2" selected>This is Option 2 </option>
(This front symbol is "<")_/select>
 
Upvote 0
If you put a space after the < , it seems to display.

< _select id = "selectlist" name = "selectlist" class = "c23_normalText">
< _option value = "1" selected>This is Option 1
< _option value = "2" selected>This is Option 2
< _/select>
 
Upvote 0
Hello 23izkool,

Can you post the URL so I can see the page source?

Sincerely,
Leith Ross
 
Upvote 0
Try getting a reference to the dropdown like this.
Code:
Dim objSelList As Object
 
    Set objSelList = appIE.document.getElementByID("selectlist")

Then you should be able to selet the item you want but I'm not 100% sure how that works.

For example this has worked for me in the past, where 1 is the index of the item to select.
Code:
objSelList.Selected(1) = True
But I'm sure there are other ways of doing it, perhaps it depends on what type of page you are dealing with.
 
Upvote 0
Thanks Norie. I tried your code but got an error message 'Run-time error '91':

Object variable or With block variable not set"

Can you teach me how to go about resolving this? Thanks!
 
Upvote 0
Hi Leith Ross,

Sorry it's something like an intranet & I'm thus unable to copy out the whole source code.

But if you can tell me what to look out for / show in this forum, I wil be be glad to do so. Really hope to get this done!

Thanks!
 
Upvote 0
Have you checked the id of the dropdown?

I used the one in what you posted in the code I posted.

Also, are you allowing th e page to fully load.

You need to do that to be able to work with the page.

Perhaps if you posted what code you have so far we can give you some pointers.:)
 
Upvote 0
HI everyone, this is the whole code which I am using when I tried to select the option value and a few points which I considered - hope it helps give some brilliant ideas to solve my problem!!

  • The macro below will print on the main worksheet the names of all the option value and select list
  • BUT it works on the below website & NOT my company intranet website
  • I noticed my company website uses java scripting but I am not sure which info I shld pick out to share here to get some solutions. If you want to look at any part, pls let me know & i will type it out line by line here...just wants to get this going!
  • Also, I have been seekin help and understand perhaps where the dropdown list sits in will matter --> i.e. which forms but when I use '.document.forms(0).item("selectlist").value="1", I keep getting error like "runtime error 91" & "object variable or with block variable not set"
  • Hope someone can give some ideas on this!
  • I am on the verge of using sendkeys which I'm sure will be loads of problems given e instability of sendkeys..
  • Hope someone can help on this!


Sub SelectFromDropDownList()
Dim appIE As InternetExplorer, btnSelect As MSHTML.HTMLSelectElement
Dim btnOption As MSHTML.HTMLOptionElement, ElementCol As MSHTML.IHTMLElementCollection
Dim ElementCol1 As MSHTML.IHTMLElementCollection
Application.ScreenUpdating = False
Set appIE = New InternetExplorer
With appIE
.Visible = True
.navigate "http://app.lta.gov.sg/home_list.asp"
End With
Do While appIE.Busy Or appIE.Readystate <> Readystate_Complete
DoEvents
Loop
' In LTA job search webpage, go to the drop down list "Select by Qualification" and make a selection.
'===========================================================================================================
Set ElementCol = appIE.Document.getElementsByTagName("Select")
For Each btnSelect In ElementCol
If btnSelect.Name = "Min_Qualification" Then
'Within the "Select" element named "Min_Qualification", group all the "Option" element in "ElementCol1"
Set ElementCol1 = btnSelect.getElementsByTagName("Option")
For Each btnOption In ElementCol1
' There are 5 options to select from: "" i.e. the null option,
' "Degree",
' "Diploma",
' "Post-Graduation" and
' "GCE-A/N/O/ITE"
' The webpage's default choice is ""
If btnOption.Value = "GCE-A/N/O/ITE" Then 'Select the option "GCE-A/N/O/ITE"
btnOption.Selected = True
End If
Next btnOption
End If
Next btnSelect
'===========================================================================================================
Application.ScreenUpdating = True
End Sub
Sub GetOptionValue()
' This subroutine prints out the option value of each of the "Select" element onto the second worksheet
' The first row contains the ID of each "Select" element, the columns under the first row contains the
' respective option values and the text associated to each option value
Dim appIE As InternetExplorer, btnSelect As MSHTML.HTMLSelectElement
Dim btnOption As MSHTML.HTMLOptionElement, ElementCol As MSHTML.IHTMLElementCollection
Dim ElementCol1 As MSHTML.IHTMLElementCollection, i As Long, j As Long, URL As String
Application.ScreenUpdating = False
URL = "http://app.lta.gov.sg/home_applicat...62es42m64lth039d7rn80l91rp9yxqxdhiwvww&source="
Set appIE = New InternetExplorer
With appIE
.Visible = True: .navigate URL
End With
Do While appIE.Busy Or appIE.Readystate <> Readystate_Complete
DoEvents
Loop
' Get the option values for each of the "Select" element in the webpage
'===========================================================================================================
j = 1
Set ElementCol = appIE.Document.getElementsByTagName("Select")
For Each btnSelect In ElementCol
i = 0
Worksheets(2).Cells(1, j) = btnSelect.Name
Set ElementCol1 = btnSelect.getElementsByTagName("Option")
For Each btnOption In ElementCol1
i = i + 1
Worksheets(2).Cells(i + 1, j) = btnOption.Value
Worksheets(2).Cells(i + 1, j + 1) = btnOption.Text
Next btnOption
j = j + 2
Next btnSelect
'===========================================================================================================
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Did you try ky suggestion of GetElementByID?

By the way you shouldn't need to worry about the script.

I don't think I've seen many web pages that didn't have some.

Also, I've never found a page where it was needed to do anything with the script.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top