lundi 30 juillet 2018

VBA selecting a value from dropdown box on a webpage

I'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




Aucun commentaire:

Enregistrer un commentaire