我一直在努力尝试使用 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);
}
在 Firefox 中,此解决方案一直有效,直到 SendKeys 调用点挂起并继续执行下一步,而无需实际触发 select2 的事件来填充所选项目。
我也厌倦了使用具有类似结果的http://code.google.com/p/selenium/wiki/AdvancedUserInteractions api。
有没有人遇到过类似的问题?