Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying a select a text from the Dropdown Menu using Selenium webdriver in C# It is working perfectly with Chrome browser, but not with Firefox. Could any one help me to fix this.

The code I am using is given below.

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);
}

I am able to call this function and this is executing successfully without any error messages. I am not able to select the expected keyword eventhough it exists.

Currently I am having a workaround of Clicking on the same id twice and that makes the code to work. With the commented section uncommented But I dont think that is correct workaround. Let me know your thoughts on this.

Thanks

share|improve this question
    
Try updating both selenium and firefox to the latest. Sometimes, mismatch causes these types of issues. –  iMatoria Aug 1 '12 at 18:36
    
The .Click() that you're performing may be interfering with the selection. Try commenting that line out and see if works. –  Michael Bautista Aug 1 '12 at 22:23
    
Have you tried using selenium Actions to perform the clicking operations. –  Hari Reddy Aug 2 '12 at 6:14
    
Hi iMatoria - Will try the same. "MPBSDSU" - This same code is working fine in chrome and it is not working only in firefox. So if I am afraid to comment the click. "Hari REddy" - Could you provide more input please. –  Vinee Aug 2 '12 at 10:40

2 Answers 2

up vote 0 down vote accepted

Yah, it doesn't work well with Firefox. I had to use this as a workaround using jQuery. Feel free to modify this code with regular JavaScript if you don't have jQuery on the page.

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
}
share|improve this answer
    
Thanks MacGyver. Will try and let you know. –  Vinee Aug 7 '12 at 11:59

Normally the select class handles the selection without you even clicking the drop down. It should work both in FF and Chrome, Ie has some other issues with Select. Try not to click the drop-down button at all. If Select class does not function the way it suppose to try clicking and navigating options sending up down keys and then pressing enter.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.