简体   繁体   中英

C#: Selecting Dropdown Items with Marionette Driver

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?

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...:)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM