0

i am trying to select value in website drop and i try so many way but nothing works I am stuck at one point where I need to select value from the drop-down box I have four drop down and i want to select option drop down then goto next drop down and ima stuck at first drop down

Sub Select_dropdown_item()
Dim IE As InternetExplorer
Dim drp As Variant
dname = Range("A1").Value
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "https://anyror.gujarat.gov.in/"
    While IE.Busy = True Or IE.readyState <> 4: DoEvents: Wend
    IE.document.getElementById("Button1").Click

If IE.LocationURL = "https://anyror.gujarat.gov.in/CustomError.htm" Then
IE.Quit
MsgBox ("website can not comunicate with server")
Else
    ''if the script fails, activate the delay
'Application.Wait Now + TimeValue("00:00:05")

End If
Set drp = IE.document.getElementById("ddlDistrict")
For x = 0 To drp.Options.Length - 1
If drp.Options(x).Value = dname Then
drp.selectedIndex = x
Exit For
End If
Next
End Sub

it show error 13 type mismatch on line

Set drp = IE.document.getElementById("ddlDistrict")

so plz help this is my html code for first drop down

    <option selected="selected" value="0">પસંદ કરો</option>
    <option value="01">કચ્છ</option>
    <option value="02">બનાસકાંઠા</option>
    <option value="03">પાટણ</option>
    <option value="04">મહેસાણા</option>
    <option value="05">સાબરકાંઠા</option>
    <option value="06">ગાંધીનગર</option>
    <option value="07">અમદાવાદ</option>
    <option value="08">સુરેન્દ્રનગર</option>
    <option value="09">રાજકોટ</option>
    <option value="10">જામનગર</option>
    <option value="11">પોરબંદર</option>
    <option value="12">જુનાગઢ</option>
    <option value="13">અમરેલી</option>
    <option value="14">ભાવનગર</option>
    <option value="15">આણંદ</option>
    <option value="16">ખેડા</option>
    <option value="17">પંચમહાલ</option>
    <option value="18">દાહોદ</option>
    <option value="19">વડોદરા</option>
    <option value="20">નર્મદા</option>
    <option value="21">ભરુચ</option>
    <option value="22">સુરત</option>
    <option value="23">ડાંગ</option>
    <option value="24">નવસારી</option>
    <option value="25">વલસાડ</option>
    <option value="26">તાપી</option>
    <option value="27">દેવભુમિ દ્વારકા</option>
    <option value="28">મોરબી</option>
    <option value="29">ગીર સોમનાથ</option>
    <option value="30">બોટાદ</option>
    <option value="31">અરવલ્લી</option>
    <option value="32">મહિસાગર</option>
    <option value="33">છોટાઉદેપુર</option>

2 Answers 2

1

Use a css attribute=value selector and concatenate in the variable

ie.document.querySelector("[value='" + dname + "']").Selected = True

If more than one dropdown has this value, and provided your given id is correct and for the appropriate dropdown, you may wish to add the parent id to isolate the appropriate dropdown

ie.document.querySelector("#ddlDistrict [value='" + dname + "']").Selected = True 
3
  • now it show object required error on line ie.document.querySelector("#ddlDistrict [value='" + dname + "']").Selected = True plz help Commented Oct 24, 2019 at 9:33
  • do you have a proper page load wait before? Does the error go away if you step slowly through code pressing F8?
    – QHarr
    Commented Oct 24, 2019 at 11:27
  • Did you step through with F8?
    – QHarr
    Commented Nov 1, 2019 at 10:03
0

I have executed the code on my machine and it is working fine in debug mode. I'd suggest to add a wait of 5-10 seconds before the line in which you are getting the drop down object.

**Application.Wait Now + TimeValue("00:00:05")**
Set drp = IE.document.getElementById("ddlDistrict")
For x = 0 To drp.Options.Length - 1
If drp.Options(x).Value = dname Then

It should work fine but will depend on the server lag and your internet speed as well.

1
  • i use advise now it show object required error on line For x = 0 To drp.Options.Length - 1 so what do i do plz help Commented Oct 24, 2019 at 9:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.