我一直在试图从使用selenium webdriver的ajax启用的select2选择列表中选择一个选项.我已经设法使用IE webdriver而不是firefox.这是我对IE的hacky解决方案
public static void SetSelect2Option(this IWebDriver driver, By locator, string subContainerClass, string searchTerm, TimeSpan? ajaxWaitTimeSpan = null)
{
var select2Product = driver.FindElement(locator);
select2Product.Click();
var searchBox = driver.FindElement(By.CssSelector(subContainerClass + " .select2-input"));
searchBox.SendKeys(searchTerm);
if (ajaxWaitTimeSpan != null)
{
driver.Manage().Timeouts().ImplicitlyWait(ajaxWaitTimeSpan.Value);
}
var selectedItem = driver.FindElements(By.CssSelector(subContainerClass + " .select2-results li")).First();
selectedItem.Click();
selectedItem.SendKeys(Keys.Enter);
}
Run Code Online (Sandbox Code Playgroud)
在Firefox中,此解决方案一直运行到SendKeys调用的位置,它只是挂起并继续下一步而不实际触发select2的事件来填充所选项目.
我也厌倦了使用http://code.google.com/p/selenium/wiki/AdvancedUserInteractions api,结果相似.
有没有人遇到过类似的问题?