我正在尝试使用C#中的Selenium webdriver从下拉菜单中选择一个文本,它在Chrome浏览器上工作得很好,但在火狐浏览器上却不行。有人能帮我解决这个问题吗。
下面给出了我使用的代码。
public void SelectCountry1(string country)
{
var countryDropDown = Driver.FindElement(By.XPath(xpathidofthecountrydropdown));
countryDropDown .Click();
//Driver.FindElement(By.XPath(xpathidofthecountrydropdown)).Click;
var selectElement = new SelectElement(countryDropDown);
selectElement.SelectByText(country);
}
我能够调用这个函数,并且它正在成功执行,没有任何错误消息。我无法选择预期的关键字,即使它存在。
目前,我有一个变通的办法,就是在同一个id上点击两次,这样代码就可以工作了。注释部分未注释,但我认为这不是正确的解决方法。让我知道你对此的想法。
谢谢
发布于 2012-08-07 06:56:14
是的,它在Firefox上不能很好地工作。我不得不使用jQuery来解决这个问题。如果页面上没有jQuery,请随意使用常规JavaScript修改此代码。
public static void SetDropdownSelectedOptionByText(IWebDriver driver, string tagId, string newText, int sleepTime)
{
// not working when selecting client id of certain types of ASP.NET user controls
//new SelectElement(driver.FindElement(By.Id(tagId))).SelectByText(newText);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("$('#" + tagId + " option:contains(" + Element.NonNullValue(newText) + ")').attr('selected', 'selected')");
js.ExecuteScript("$('#" + tagId + "').change()");
System.Threading.Thread.Sleep(sleepTime); // wait for dependent dropdown to load its values
}
发布于 2012-08-03 22:23:53
通常情况下,select类甚至不需要单击下拉菜单就可以处理选择。它应该可以在FF和Chrome中工作,即Select有一些其他问题。尽量不要点击下拉按钮。如果Select class不能按预期的方式工作,则尝试单击并导航选项,向上发送向下键,然后按enter键。
https://stackoverflow.com/questions/11763624
复制相似问题