0

I am trying to loop through the options or items of a drop down menu, but I don't know the number of items as it will be changeable every time.

Here's the html part of the sList3

<select name="ctl00$ContentPlaceHolder1$Dschool" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Dschool\',\'\')', 0)" id="ContentPlaceHolder1_Dschool" style="font-size:12pt;font-weight:bold;width:500px;">
	<option selected="selected" value="0"> Select From Menu </option>
	<option value="311223">first option</option>
	<option value="311625">some option</option>
</select>

Here's my code that I have started

For i = 1 To 4
    Set sList1 = .FindElementById("ContentPlaceHolder1_Dedara").AsSelect
    sList1.SelectByIndex i
    .Wait 2000
    Set sList2 = .FindElementById("ContentPlaceHolder1_Drel").AsSelect
    sList2.SelectByIndex 1
    .Wait 2000
    Set sList3 = .FindElementById("ContentPlaceHolder1_Dschool").AsSelect

    'How can I loop through the options (unknown in length)
Next i

I would like to loop each option and debug.print the value of the option.

SOLUTION

With the help of JeffC this is the final solution

            For j = 1 To sList3.Options.Count
            Debug.Print sList3.Options(j).Text
        Next j
6
  • I think you can for each through the sList3.children() or something to that effect? That's how it works with an HtmlDocument iirc Commented Dec 20, 2018 at 17:00
  • I tried For Each e In sList3.Children() but didn't work and I got an error. Commented Dec 20, 2018 at 18:03
  • Is there no Options list hanging off the .AsSelect? If nothing else, you could count the number of OPTIONs that are children of the SELECT. Commented Dec 20, 2018 at 18:03
  • Thanks a lot. I didn't get what you mean? Can you give me line of code to count the options as I noticed some of those lists are empty and with no options .. Commented Dec 20, 2018 at 18:10
  • 1
    I don't do VBA Selenium ... I use C# (and formerly Java). In C#, we have new SelectElement(driver.FindElement(...)).Options which is a list of elements. You can slap a .Count on the end of that and get the number of options. I don't know if you have the equivalent in VBA but I'm assuming you do since you have a .AsSelect. If I were to guess, it would be something like .FindElementById(...).AsSelect.Options. If you have Intellisense, when you type . after .AsSelect you should get a list of methods/properties. Look through that list and see what you can find. Commented Dec 20, 2018 at 18:16

2 Answers 2

2

I think you can use for each loop

WebElement selectElement = driver.findElement(By.xpath("//select[@id='ContentPlaceHolder1_Dschool']"));
Select select = new Select(selectElement);
List<WebElement> options = select.getOptions();
for (WebElement we : options) 
{
  System.out.println("Element="+we.getText());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok.. I don't do VBA selenium..I use selenium WebDriver in Java
I think the solution posted in my post is similar to that used in Java. Am I right?
1

You can use CSS selector to get collection of the options under the parent id

Dim elements As Object, element As Object
Set elements = driver.FindElementsByCss("#ContentPlaceHolder1_Dschool option")

Then loop

For Each element In elements:
    Debug.Print element.text
Next

CSS:


If the id is dynamic switch css to

[id^=ContentPlaceHolder1] option

5 Comments

Thanks a lot. At this line Set elements ... I got error of doesn't support this object. And I can't find findElementsByCssSelector method in excel VBA for selenium.. I have played around it and get it work using FindElementsByCss' instead of findElementsByCssSelector `
Sorry I got confused with python syntax and did a sort of hybrid. lol. Let me update
You know you can also post your own answer and accept after two days.
But this was JeffC's answer and I just followed his steps
Up to you but may be useful for other readers and comments can fade away over time. Css selectors will be faster as will For Each loop but your solution is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.