I want to select options under particular optgroup from a multiselect picklist field, for example
<select id="xyz">
<optgroup label="Group1">
<option value="1">pick1</option>
<option value="2">pick2</option>
</optgroup>
<optgroup label="Group2">
<option value="3">pick3</option>
<option value="4">pick4</option>
</optgroup>
</select>
if the picklist field is like above, I can use
new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick1");
new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick2");
for selecting all the options which is under optgroup -Group1 from the above.
Here I know which are all the options present in the Group1 and hence I can select like above.
But my requirement is dynamically I need to select all the options under optgroup-Group1.
If I want to select all the options both including Group 1 & 2 dynamically
Then I can use
new Select(driver.findElement(By.id("xyz"))).getOptions();
to get all options as a List and I can store it as an array, and I can select all the Options one by one using array
But how can I get all the options under only one specific optgroup say Group1 or Group2?
You should use xpath and
findElements: