1

Not a duplicate: I am not asking for the <option> within a <select>. I would like to return the <select> web elements themselves as a List. When achieved, the list should have a size of 2.


I have only found driver.findElements() that returns a List<WebElement>.

How do I return a List<Select> by ng-model="income.frequency"?

(Note: I have ngWebDriver so I can use ByAngular.model("income.frequency") as locator.)

HTML: enter image description here

k_rollo
  • 5,304
  • 16
  • 63
  • 95
  • Possible duplicate of [Read option text from drop down values in selenium](https://stackoverflow.com/questions/38304586/read-option-text-from-drop-down-values-in-selenium) – Jobin Jan 22 '18 at 11:51

1 Answers1

4

In order to match elements by their attribute, you can use the CSS selector:

By.cssSelector("<tag>[<attribute>='<value>']")

And in context:

List<WebElement> elements = driver.findElements(By.cssSelector("select[ng-model='income.frequency']")); 


Edit: According to Selenium documentation, you can create a Select with a very straight-forward constructor. Hence, if you want a list of Select objects, just construct a new list:

List<Select> selectList = new ArrayList<>();
List<WebElement> elements = driver.findElements(By.cssSelector("select[ng-model='income.frequency']")); 
for(WebElement element : elements) {
    selectList.add(new Select(element);
}
GalAbra
  • 5,048
  • 4
  • 23
  • 42
  • 2
    If you'd like a list of `Select`s, you can use its constructor with each WebElement: `new Selector(WebElement element)` – GalAbra Jan 22 '18 at 12:10
  • @silver Hi! I've edited my answer. Let me know if it works for you – GalAbra Jan 22 '18 at 12:15
  • 1
    Thank you! Selenium doesn't seem to have this by default yet but your solution is elegant. Upvoted and accepted. – k_rollo Jan 22 '18 at 12:38