2

I'm trying to do a simple task of trying to select a value in a dropdown by using the text shown. The scenario is as below.

My HTML looks like.

<div id="TestContainer" class="col-md-4">
    <select onchange="Test()">
        <option>Test1</option>
        <option>Test2</option>
        <option>Test3</option>
        <option>Test4</option>
    </select>
</div>

By using selenium i want to use the second item in the dropdown that is test2. C# code which i have written for the same is.

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
string localURL = "http://localhost:82/";

using (IWebDriver driver = new FirefoxDriver(service))

  {
         driver.Navigate().GoToUrl(localURL);
         var div = driver.FindElement(By.Id("TestContainer"));
         div.Click();
         IWebElement dropDownListBox = div.FindElement(By.TagName("select"));
         SelectElement demoSelect = new SelectElement(dropDownListBox);
         demoSelect.SelectByText("Test2");
         driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));

   }

Apart from the above i have even tried iterating the options one by one and selecting the appropriate item like below also to no avail.

if (option.Text.Equals("Test2"))
{
    option.Click();
           driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));
    break;
 }

In both the above cases, The code doesn't break and no exception is thrown but the value will not be selected and nothing seems to be happening.

The version of selenium i'm using is as below.

<package id="Selenium.Support" version="2.53.1" targetFramework="net452" />
<package id="Selenium.WebDriver" version="2.53.1" targetFramework="net452" />
<package id="WebDriver.GeckoDriver"version="0.9.0"targetFramework="net452" />

Also i'm using the latest version of firefox (48.0)

Has anyone faced this issues before? It would be great if you could point me in the right direction.

9
  • As a side note, I think you misunderstood the use of ImplicitlyWait. Go over this
    – Guy
    Aug 9, 2016 at 10:48
  • @Viswas Menon Just try placing a break point after demoSelect.SelectByText("Test2");, debug and see if 'Test2' was selected. Also please check if it is working manually. I could see there is ` onchange="Test()"` may be this might be creating a problem.Think in that area also.
    – Siva
    Aug 9, 2016 at 11:02
  • @Guy Thanks for pointing that one out.. Aug 9, 2016 at 11:26
  • @Siva i had tried those out, The value is not getting set even though no error happens, its not set as the selected option nor does the value. The Javascript method as of now is just alerting something and it works otherwise so dont think that is the problem, thanks. Aug 9, 2016 at 11:28
  • 1
    Possible duplicate of How to Using Webdriver Selenium for selecting an option in C#?
    – JeffC
    Aug 9, 2016 at 15:48

3 Answers 3

3

If you have tried all methods of SelectElement to select an option but didn't get success, Here is another solution to try using IJavascriptExecutor as below :-

 IWebElement dropDownListBox = driver.FindElement(By.cssSelector("#TestContainer select"));
((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", dropDownListBox, "Test2");

Full code :

using (IWebDriver driver = new FirefoxDriver(service))  
  {
      driver.Navigate().GoToUrl(localURL);
      IWebElement dropDownListBox = driver.FindElement(By.cssSelector("#TestContainer select"));
      ((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", dropDownListBox, "Test2");

   }
7
  • i did try this option now.. but that did not help as well. the value in the select is not changing. Should i make the driver wait or something Aug 11, 2016 at 6:29
  • stackoverflow.com/questions/38256147/… , This issue i'm facing is very similar to the above mentioned question, Except in that the solution you provided seemed to have worked for Mike. Aug 11, 2016 at 6:34
  • @ViswasMenon once execute this javascript on browser console var select = document.querySelector("#TestContainer select"); for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == "Test2"){ select.options[i].selected = true; } } and let me know Aug 11, 2016 at 6:35
  • that executes correctly and selects Test2 as expected. Aug 11, 2016 at 6:52
  • @ViswasMenon that means script is correct, just check your locator to point correct select box or not.. Aug 11, 2016 at 6:53
1

I was having this exact problem also.

<package id="Selenium.Support" version="2.53.1" targetFramework="net452" />
<package id="Selenium.WebDriver" version="2.53.1" targetFramework="net452" />
<package id="WebDriver.GeckoDriver"version="0.9.0"targetFramework="net452" />

And running FireFox version 48.0..

After updating FireFox to version 49.0.1, the SelectElement class was finally able to do its job.

1

Javascriptexecutor is detectable by the website, I was having the same problem and I solved it creating a webelement dropdown. Here is the code:

WebElement dropdown = driver.findElement(By.id("serverLogin")); 
    dropdown.sendKeys(server);  
    dropdown.sendKeys(Keys.ENTER);

This way there is no need to upgrade firefox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.