问题
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?
回答1:
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...:)
回答2:
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
来源:https://stackoverflow.com/questions/38256147/c-selecting-dropdown-items-with-marionette-driver