fabioandrec / selenium-vba

Automatically exported from code.google.com/p/selenium-vba
0 stars 0 forks source link

Need help getting selected text from a drop down #36

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Operating system : Win 7
.Net Framework version :
Office Version : 2010
SeleniumWrapper version : 1.0.14.0

What is your issue ?
I need help writing code to get the selected option from a drop-down field.
for test option 3, selected = "selected" and i wish to get the text "test 
option 3" in a variable. Can you please help me with the code?

HTML code of the field:-
<select size="1" id="Test_Reason" name="TestReason">
<option value=""></option>
<option value="93pVU5TE6m+3DXeaAdGauVpfBTBgK79u9UZuZX7usaI=">test option 
1</option>
<option value="V9h+SaJ+KSgQraaaGtFwl3+bcOBTSq+7RxXnKuhsBZ9=">test option 
2</option>
<option value="vQPa7USX8tLTcpZZ7uS6KjNarTv+fxJGKKaHNVIzWGE=" 
selected="selected">test option 3</option>
<option value="TjTaPhg3aGUbqSbad9aaN91JNJHvuS73FuThv5tbJ99=">test option 
4</option>
</select>

Original issue reported on code.google.com by rohan.ka...@gmail.com on 26 Dec 2013 at 6:35

GoogleCodeExporter commented 9 years ago
I understand that i can get all the options in an object collection:-
objCollection = driver.findElementsByCssSelector("#Test_Reason option")

and then run a loop to find the option thats selected:-
For j = LBound(objCollection) To UBound(objCollection)
     If objCollection(j).Selected Then x = objCollection(j).Text
Next

but is there a direct way to get this text without running a loop?

Original comment by rohan.ka...@gmail.com on 26 Dec 2013 at 7:32

GoogleCodeExporter commented 9 years ago
There you go :

...
    Dim selectedText

    'Selenium 2 :
    selectedText = driver.findElementById("Test_Reason").AsSelect.SelectedOption.Text
    'Selenium 1 :
    selectedText = driver.getSelectedLabel("id=Test_Reason")

    Debug.Print selectedText
...

Original comment by florentbr on 26 Dec 2013 at 7:40

GoogleCodeExporter commented 9 years ago
Thank you for the response!!

Original comment by rohan.ka...@gmail.com on 26 Dec 2013 at 9:13

GoogleCodeExporter commented 9 years ago

Original comment by florentbr on 8 Sep 2014 at 5:40