2012-08-01 96 views
2

我想从下拉菜单中使用C#中的Selenium webdriver选择一个文本# 它与Chrome浏览器完美配合,但与Firefox无法正常工作。任何人都可以帮我解决这个问题。Selenium Webdriver C# - 未选择Firefox下拉菜单文本

我使用的代码如下。

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,并使代码工作。评论部分未注释但我不认为这是正确的解决方法。让我知道你的想法。

感谢

+0

尝试更新硒和Firefox的最新。有时,不匹配会导致这些类型的问题。 – iMatoria 2012-08-01 18:36:30

+0

您正在执行的.Click()可能会干扰选择。试着评论一下,看看是否有用。 – 2012-08-01 22:23:37

+0

您是否尝试过使用硒动作来执行点击操作。 – 2012-08-02 06:14:58

回答

0

是啊,它不适用于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 
} 
+0

谢谢MacGyver。会尝试让你知道。 – Vinee 2012-08-07 11:59:17

0

通常情况下,选择类处理的选择没有你甚至不点击下拉。它应该可以在FF和Chrome中使用,即使在Select中也有其他问题。尽量不要点击下拉按钮。如果Select类没有按照假想的方式运行,那么尝试点击并导航选项,然后按下回车键。

相关问题