Select option from dropdown and Webdriver in C#

5,837 views
Skip to first unread message

gadhet

unread,
Nov 7, 2011, 9:44:36 AM11/7/11
to webdriver
Hi all,

I would like to select some option from my dropdown by text from
dropdown not via value. Can someone help me with this?

I have part of my code:

<td class="pageContentControl">
<select
id="ctl00_ctl20_g_6db904a7_390a_4cdb_9cf5_6fd8dfbcd9f3_ctl00_comboSelectPage"
style="width:250px;"
onchange="javascript:setTimeout('__doPostBack(\'ctl00$ctl20$g_6db904a7_390a_4cdb_9cf5_6fd8dfbcd9f3$ctl00$comboSelectPage
\',\'\')', 0)"
name="ctl00$ctl20$g_6db904a7_390a_4cdb_9cf5_6fd8dfbcd9f3$ctl00$comboSelectPage">
<option value="38" selected="selected">Expat Portal</option>
<option value="39">Corporate information</option>
<option value="40">Mission statement</option>
<option value="41">Types of assignments</option>
<option value="42">Objectives</option>
....

and now I would like to type e.g. Objectives and select this option
from my dropdown by selenium/webdriver.

I have the same action by value but I'd like to change this to text
from dropdown:
driver.FindElement(By.CssSelector("option[value=\"42\"]")).Click();

I'm using C#. Can someone help? Thanks

Mark Collin

unread,
Nov 7, 2011, 11:28:25 AM11/7/11
to webd...@googlegroups.com
You can use the Select object available in selenium support:

import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

Select mySelectElement = new
Select(driver.findElement(By.xpath("//select[@id='foo']")));
mySelectElement.selectByVisibleText("My Text");

or

mySelectElement.selectByValue("Value of Option");

Hi all,

--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Jim Evans

unread,
Nov 7, 2011, 12:15:58 PM11/7/11
to webdriver
Note that in the .NET bindings, the dropdown helper class is called
SelectElement (Select is a keyword when using LINQ). It's located in
the WebDriver.Support.dll assembly.
> For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.
>
> --
> This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
>
> If you have received this email in error please notify postmas...@ardescosolutions.com

gadhet

unread,
Nov 8, 2011, 5:42:06 AM11/8/11
to webdriver
so I've done something like this:

C#:

SelectElement mySelectElement = new
SelectElement(driver.FindElement(By.XPath("//
select[@id='ctl00_ctl20_g_6db904a7_390a_4cdb_9cf5_6fd8dfbcd9f3_ctl00_comboSelectPage']")));
mySelectElement.SelectByText("Objectives");

but it doesn't select Objectives option from dropdown. Any suggestion?

gadhet

unread,
Nov 8, 2011, 6:01:05 AM11/8/11
to webdriver
Sorry my mistake. It's working.

Thanks!!!
Reply all
Reply to author
Forward
0 new messages