getElementsByName VBA Issue

bjurney

Active Member
Joined
Aug 24, 2009
Messages
320
I have been working on automating a webpage with information based on the values in Excel, its going pretty well except that I ran into an element that does not have an ID, only a name. I tried the following code without success:

Code:
IntExpl.Document.getElementsByName("PICKUPHOUR2").Value = 7

The element is a dropdown menu if that helps. I dont know much about HTML, so you're my only hope!

The HTML code looks like this:

Code:
<select name="PICKUPHOUR2">[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="1" selected="">1[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="2">2[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="3">3[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="4">4[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="5">5[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="6">6[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="7">7[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="8">8[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="9">9[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="10">10[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="11">11[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]<option value="12">12[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]  [/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]</option></select>

 
If the form element is a drop-down box, I believe your problem is there is not a 'value' attribute but rather a SelectedIndex attribute.
Thus you may have better luck with the following...

Code:
IntExpl.Document.getElementsByName("PICKUPHOUR2").SelectedIndex = 7

I can't test this and it's been a bit since I've automated IE, so I'm not 100% confident in my answer, but it's simple enough to test. :nya:
 
Upvote 0
I've run into this before as well. what are you running for IE? getElementByName only works for IE9 and above. unfortunately I am not sure of a workaround IE8. If you come across one be sure to post it here!

~Matt
 
Upvote 0
I am on IE11. I tried the selectedindex that BiocideJ suggested, but I still continue to get the debugging error saying that the object doesn't support this property or method.
 
Upvote 0
getElementsByName returns a collection of elements, even if there is only one element with the specified name you'll need to specify the index to get the element you want.

The index is 0 based so try this.
Code:
IntExpl.Document.getElementsByName("PICKUPHOUR2")(0).SelectedIndex = 6
PS The index of the dropdown is also 0 based, that's why I've used 6 instead of 7.
 
Last edited:
Upvote 0
Apologies to jump on the back of this query but I have a very similar problem.

I am using
Code:
ie.document.getElementsByName("ad_award_name")(0).selectedIndex = 1 

ie.document.getElementsByName("ad_award_name")(0).FireEvent ("onchange")

I am not getting an error messge BUT nothing is changing on the webpage i.e. nothing is being selected from the dropdown. I am using internet explorer 11 if that makes any difference?

Any ideas?
 
Upvote 0

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