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.

Viswas Menon
  • 310
  • 3
  • 11
  • As a side note, I think you misunderstood the use of `ImplicitlyWait`. Go over [this](http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits) – Guy Aug 09 '16 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 09 '16 at 11:02
  • @Guy Thanks for pointing that one out.. – Viswas Menon Aug 09 '16 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. – Viswas Menon Aug 09 '16 at 11:28
  • @ViswasMenon, I guess you would have already tried other methods of `SelectElement` - `SelectByIndex`... If Not Try them.Also you can see if the element is rightly identified by the `IWebElement dropDownListBox = div.FindElement(By.TagName("select"));` – Siva Aug 09 '16 at 12:09
  • @Siva yes i have already tried the other option such has SelectByIndex, SelectByValue and yes its identifying the right element. – Viswas Menon Aug 09 '16 at 12:21
  • @ViswasMenon, i thing the option left is to run javascript and set its value – Siva Aug 09 '16 at 12:26
  • 1
    Possible duplicate of [How to Using Webdriver Selenium for selecting an option in C#?](http://stackoverflow.com/questions/5278281/how-to-using-webdriver-selenium-for-selecting-an-option-in-c) – JeffC Aug 09 '16 at 15:48
  • @JeffC I had seen that post and had tried that options which are mentioned. Think they had used a previous version of selenium. – Viswas Menon Aug 10 '16 at 03:27

3 Answers3

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

   }
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 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 – Viswas Menon Aug 11 '16 at 06:29
  • http://stackoverflow.com/questions/38256147/c-selecting-dropdown-items-with-marionette-driver , 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. – Viswas Menon Aug 11 '16 at 06: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 – Saurabh Gaur Aug 11 '16 at 06:35
  • that executes correctly and selects Test2 as expected. – Viswas Menon Aug 11 '16 at 06:52
  • @ViswasMenon that means script is correct, just check your locator to point correct select box or not.. – Saurabh Gaur Aug 11 '16 at 06:53
  • @ViswasMenon you are clicking first on div with id `TestContainer ` then find the select box.. just remove everything and try exact updated code and let me know.. – Saurabh Gaur Aug 11 '16 at 06:56
  • That worked perfectly locally, Thank you very much.. Actually this was a test project to get a similar scenario working in a external site.. The same exact code in the external site doesnt work for some reason..:(, But the above code was really useful. thanks. – Viswas Menon Aug 11 '16 at 07:19
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.

MackyD
  • 11
  • 2
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

Davide Melis
  • 39
  • 1
  • 8