Select option from OptionGroup

1,066 views
Skip to first unread message

Piotr K

unread,
Nov 23, 2011, 6:06:24 AM11/23/11
to Selenium Users
Hi all,

I have following problem I would like to choose one option but I don't
know how to manage with this optgroup :(
Can someone help me choose some option via name e.g.

"Ability to manage complexity"

<div
id="ctl00_ctl32_g_9183e608_ac2c_41cc_b6dc_da0c1d8f4883_ctl00_panelRedCard">
<span
id="ctl00_ctl32_g_9183e608_ac2c_41cc_b6dc_da0c1d8f4883_ctl00_lbRedCard"
class="upload_label">Red Card</span>
<br>
<select id="chzn-select" class="chzn-done" tabindex="-1"
multiple="multiple" style="width: 350px; display: none;" data-
placeholder="Select...">
<option value=""></option>
<optgroup label="Commitment - group. Categories inside the group:">
<option>Industry and application knowledge</option>
<option>Ability to manage complexity</option>
<option>Partnering for mutual benefit</option>
<option>Ease of doing business</option>
<option>Health &amp; Safety</option>
</optgroup>
<optgroup label="Purchasing decision - group. Categories inside the
group:">
<optgroup label="Installation and operation - group. Categories inside
the group:">
<optgroup label="Service and support - group. Categories inside the
group:">
<optgroup label="Others - group. Categories inside the group:">
</select>
<div id="chzn-select_chzn" class="chzn-container chzn-container-multi"
style="width: 350px;">
<ul class="chzn-choices">
<li id="chzn-select_chzn_c_4" class="search-choice">
<span>Partnering for mutual benefit</span>
<a class="search-choice-close" rel="4" href="javascript:void(0)"></a>
</li>
<li class="search-field">
</ul>
<div class="chzn-drop" style="left: -9000px; width: 348px; top:
38px;">
</div>
<input
id="ctl00_ctl32_g_9183e608_ac2c_41cc_b6dc_da0c1d8f4883_ctl00_selectedOpt"
type="hidden"
name="ctl00$ctl32$g_9183e608_ac2c_41cc_b6dc_da0c1d8f4883$ctl00$selectedOpt"
value="Partnering for mutual benefit;">
<br>
<div class="other_Description" style="display: none;">
</div>

I was trying something like that:

driver.FindElement(By.CssSelector("ul.chzn-choices")).Click();

SelectElement mySelectElement = new
SelectElement(driver.FindElement(By.XPath("//select[@id='chzn-
select']")));
mySelectElement.SelectByText("Ability to manage
complexity");

and I've received this error:
System.InvalidOperationException : An error occurred executing the
click atom (UnexpectedJavaScriptError)

Thanks for help me.

bs

unread,
Nov 23, 2011, 7:22:18 AM11/23/11
to Selenium Users
Modify the function below:

public static boolean selectlist(String listname, String optionval)
throws Exception
{
boolean selected=false;
try{
WebElement select = driver.findElement(By.id("chzn-select"));
List <WebElement> options =
select.findElements(By.tagName("option"));
for(WebElement option : options)
{
if(option.getText().equals("Ability to manage complexity""))
{
option.click();
selected=true;
break;
}
} }

Piotr K

unread,
Nov 23, 2011, 8:01:09 AM11/23/11
to Selenium Users
I've chanhed this a little because I need it in C# not in Java

Boolean selected=false;
IWebElement select = driver.FindElement(By.Id("chzn-
select"));
IList <IWebElement> options =
select.FindElements(By.TagName("option"));
foreach(IWebElement option in options)
{
String name =
option.FindElement(By.CssSelector("li.chzn-select_chzn_c_3")).Text;
if(name == "Ability to manage
complexity" )
{
option.Click();
selected=true;
break;
}
}

and I have problem with this:
String name = option.FindElement(By.CssSelector("li.chzn-
select_chzn_c_3")).Text;

how to find this text : "Ability to manage complexity" in my html
structure. Please help me

Piotr

Reply all
Reply to author
Forward
0 new messages