Reputation: 183
enter image description hereupdatedI'm trying to use VBA to log me into a secure webpage, then navigate to a webpage where I need to select a value from a drop down box before searching the database.
I cannot get the last part where it selects the value in the drop down box to work, I have used the below code.
The drop down box Name is = District, text value is "South" and combo value for South is A in HTML code. Can someone please help (read a couple of other posts but didn't understand them).
Sub database()
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'add worksheet
Sheets.Add After:=ActiveSheet
'destination
Set destsheet = ActiveSheet
'use internet explorer
Set IE = CreateObject("InternetExplorer.application")
' with internet open, make this visable and go to webpage x, enter username
and passwork
With IE
.Visible = True
.Navigate ("URL")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
'.Document.getElementsbyname("User name").Focus
.Document.getElementsByName("username")(0).Value = "username"
.Document.getElementsByName("password")(0).Value = "Pword"
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Set objCollection = IE.Document.getElementsByTagName("input")
'log in (submit)
i = 0
While i < objCollection.Length
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'upon logging in naviage to webpage...
objElement.Click
.Navigate ("URL 2")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Debug.Print .LocationURL
End With
With IE
IE.doc.getElementsByName("district").Item(A).Selected = True
End With
End Sub
Upvotes: 1
Views: 6407
Reputation: 84465
Try
.document.querySelector("Select[name=District] option[value=A]").Selected = True
The Select[name=District] option[value=A]
is a CSS selector. It looks for elements with option
tag with attribute value
whose value = A
and with a parent element whose tag is Select
which has attribute name
with value District
. The querySelector method of document applies the selector.
Upvotes: 1