0

I am trying to print out the value I am selecting from a dropdown list.

SelectElement selector = new SelectElement(ddlRegisters);
selector.SelectByIndex(1);
String regSelect = selector.Options[1].Text.ToString();
Console.WriteLine("The User Selected  "+regSelect+".");

But this isn't working can anyone provide clarification?

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Andy Williams
  • 879
  • 1
  • 14
  • 33
  • What exactly is "isn't working"? – Guy Apr 11 '16 at 20:10
  • @Guy I am successfully able to get the index from the ddl but I am running a Console.WriteLine statement after to capture the value of what drop down text I selected. If i comment out the bottom two lines my code works as intended so I simply want to report on what I am finding. – Andy Williams Apr 12 '16 at 11:19
  • Its still not clear. You get exception? nothing is printed? something else? post **all** relevant data, including stack trace if you have. – Guy Apr 12 '16 at 11:23

2 Answers2

1

You should use code like below

 SelectElement selector = new SelectElement(ddlRegisters);
 selector.SelectByIndex(1);
WebElement option = select.GetFirstSelectedOption();
 String regSelect   option .Text
Console.WriteLine("The User Selected  "+regSelect+".");

More details go link: How to get selected option using Selenium WebDriver with Java

Community
  • 1
  • 1
0

This is no longer an issue, came back this morning to re run the code and it works. Not exactly sure what I missed yesterday but used this code:

        ddlRegisters.Click();
        SelectElement selector = new SelectElement(ddlRegisters);
        selector.SelectByIndex(1);
         String regSelect =   selector.Options[1].Text.ToString();
         Console.WriteLine("The User Selected  "+regSelect+".");
Andy Williams
  • 879
  • 1
  • 14
  • 33