1

I am using Selenium Webdriver with C# bindings and trying to switch from the old FirefoxDriver (pre-FF 47) to the new Marionette driver (FF47 and above) and it's working great after some teething problems that seemed to be fixed with the release of Selenium 2.53.1 and FF 47.0.1.

The only problem now is that it seems to have an issue selecting option tags under a select tag. The following code works for all other browsers that I am testing in (FF < 46, Chrome, IE). I am passing the following arguments into my dropdownSelect function. The select IWebElement and the text to search for. Here's the function definition:

public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText)

I have tried use the SelectElement() class as I have with all of the other browsers

select = new SelectElement(inObject);

//select the matching element
select.SelectByText(inText);

I've also tried getting a Collection of the option and scrolling through the collection using both Click():

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
ReadOnlyCollection<IWebElement> optDropdown;

optDropdown = inObject.FindElements(By.TagName("option"));

foreach (IWebElement thsItem in optDropdown)
 {
   //check for matching text
    if (thsItem.Text == inText)
     {
       // 1/4 second wait
       Thread.Sleep(250);

       thsItem.Click()

       //exit foreach loop
       break;
     }
 }

and a javascript click in place of the thsItem.Click() piece of code

//click option element
js.ExecuteScript("arguments[0].click();", thsItem);

Nothing is ever selected and no error or exception is thrown. It just continues on its merry way without selecting anything

Am I doing something wrong or is this something that is still being worked out with the new Marionette driver?

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Mike Kiewicz
  • 83
  • 1
  • 11
  • did you get the same working? I'm facing the exact same issue, I have tried every option i could find to solve this problem and any help would be great – Viswas Menon Aug 11 '16 at 05:04

2 Answers2

0

Try whole opration with ExecuteScript() as below :-

public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText) {

   IJavaScriptExecutor js = driver as IJavaScriptExecutor;
   js.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; } }", inObject, inText);

}

Hope it will work...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • That works partially. It seems that as long as there are no dependencies on the dropdown, it will work just fine. However if there are dependencies (i.e. the values in the selected dropdown are dependent upon the value in a previously populated dropdown) the value is selected and as soon as the button to submit the valuies is selected , the dropdown value in the dependent box disappears Good answer, though!! Almost there!! – Mike Kiewicz Jul 11 '16 at 15:35
  • this option is also not working for me , Did you make the driver wait ? – Viswas Menon Aug 11 '16 at 06:04
0

I figured this out by just using Javascript similar to what was described above. Since there is a dependency on this dropdown when it changes I just selected the appropriate option when it is found in Selenium and fired the onchange even with Javascript

Here's the HTML for the select box

<select class="T2FormControl"   id="ctl00_pageContent_TableList_T2DropDownList_DropDownList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$pageContent$TableList$T2DropDownList$DropDownList\',\'\')', 0)" name="ctl00$pageContent$TableList$T2DropDownList$DropDownList">

And the Javascript that performs the action

//click option element and for change event
js.ExecuteScript("arguments[0].selected = true;" +
                 "var element=arguments[1];" +
                 "var event=document.createEvent(\"HTMLEvents\");" + 
                 "event.initEvent('change', false, true);" +
                 "element.dispatchEvent(event);", thsItem, inObject);

with IWebElement thsItem being the option selected and IWebElement inObject being the select tag for the dropdown

Seems like a roundabout way to do something that the other Selenium drivers do automatically, but it works

Mike Kiewicz
  • 83
  • 1
  • 11