1

How can I get Selenium to select from an option value from a dropdown ?

The HTML is below:

<select name="cboProxyMember" id="idProxyMember" onchange="selectMember(); return false">
 <option value="29">
 Text - Fullers Inns
 <option value="8840">
 Text - Turks Head

I have selected the element with no problem, using:

 var selectproxy = Driver.Instance.FindElement(By.Id("idProxyMember"));
 selectproxy.Click();

But how do I select the option ?

I have tried a find element by name and tagname but neither work.

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Parkyster
  • 155
  • 3
  • 14

6 Answers6

2

Sorry for posting this late, Below code example will solve the issue, selecting from the dropdown.

var selectproxy = Driver.Instance.FindElement(By.Id("idProxyMember"));
selectproxy.Click();
var SelectOption = new OpenQA.Selenium.Support.UI.SelectElement(selectproxy);
selectElement.SelectByText(<Define your option name which you want to 
select>);

For example: selectElement.SelectByText("Turks Head");
1

This is an example in Python as I'm not familiar with the C# bindings, my apologies.

from selenium.webdriver.support.select import Select

selectproxy = Select(driver.find_element_by_id("idProxyMember"))
selectproxy.select_by_visible_text("Text - Fullers Inns")
selectproxy.select_by_value("29")
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • +1, the idea is the same in C# though - it's called the `SelectElement` within the `OpenQA.Selenium.Support` namespace. – Arran Apr 16 '14 at 14:49
1
Select select = new Select(driver.findElement(By.id("idProxyMember")));
select.deselectAll();
select.selectByVisibleText("Fullers Inns");
select.selectByValue("29");
Kumar
  • 61
  • 1
  • 5
  • 9
0

This posting describes how to select an option from a dropdown using C#. The initial IWebElement you locate must be converted to a Select element before you can select options from it. Select a value from drop down using Selenium WebDriver C#

Hope this helps!

Community
  • 1
  • 1
E02190
  • 11
  • 1
  • Try and avoid linking to tutorials/websites without a small sample of code as links can move or be deleted in the future. – ScottMcGready Apr 16 '14 at 13:39
0
   Correct you cant select by this because in your HTML,

It shows "onchange="selectMember();", Means ON selecting the any item from dropdown complete page has been changed. Thats why you have to use the switch to that Frame before selecting any value from dropdown.

driver.switchTo().frame(name_or_id);

Now select the value from dropdown

0

Sample statements to open browser, load URL and select value from dropdown

static WebDriver driver;
System.setProperty("webdriver.ie.driver","C:\\(Path)\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();

driver.get("EnterURLHere");          
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

Select value1 = new Select(driver.findElement(By.id("idProxyMember")));    
value1.selectByVisibleText("29");    //Select Character from dropdown list
Krushna Chulet
  • 1,625
  • 1
  • 10
  • 9