Viswas Menon
Viswas Menon

Reputation: 310

Selenium : Firefox Driver, Selecting an item from a dropdown using SelectElement in c# not working correctly

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.

Upvotes: 2

Views: 3847

Answers (3)

Davide Melis
Davide Melis

Reputation: 39

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

Upvotes: 1

MackyD
MackyD

Reputation: 11

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.

Upvotes: 1

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

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");

   }

Upvotes: 3

Related Questions